<?php

namespace app\api\controller;

use app\common\controller\Api;
use  think\Db;
use think\Env;
use fast\Random;
use app\admin\library\Auth;

/**
 * 业务端接口
 */
class Salesman extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    private $redis;
    private $url;
    private $user_arr;

    public function _initialize()
    {
        parent::_initialize();

        $this->redis = redis(Env::get('redis.host'), Env::get('redis.port'), Env::get('redis.pwd'), Env::get('redis.db'));
        $this->url = Env::get('url.url');

        $token = empty($_SERVER['HTTP_TOKEN']) ? $this->error('token不能为空',[],'401'):$_SERVER['HTTP_TOKEN'];//获取token
        $this->user_arr = json_decode($this->redis->Get('luying:token:'.$token),true);//设置redis

        if (empty($this->user_arr)){
            $this->error('用户信息为空',[],'401');
        }
    }

    /**
     *个人中心接口
     *
     */
    public function user_info(){
        $data = Db::table('fa_admin')
            ->field('id,username,nickname,avatar,mobile')
            ->where('id',$this->user_arr['id'])
            ->find();

        $this->success('查询成功',$data);
    }

    /**
     * 更换头像
     *
     */
    public function update_avatar(){
        $avatar = empty(request()->post('avatar')) ? $this->error('avatar不能为空'):request()->post('avatar');

        $update = Db::table('fa_admin')
            ->where('id',$this->user_arr['id'])
            ->update([
                'avatar' => $avatar,
                'updatetime' => time(),
            ]);

        if ($update !== false){
            $this->success('成功',[]);
        }else{
            $this->error('失败',[]);
        }


    }


    /**
     *预约挂号列表接口
     *
     */
    public function registration_list(){
        $page = empty(request()->post('page')) ? 1:request()->post('page');//获取页码
        $limt = empty(request()->post('limt')) ? 10:request()->post('limt');//获取每页显示条数

        $code = empty(request()->post('code')) ? '':request()->post('code');
        $medic_name = empty(request()->post('medic_name')) ? '':request()->post('medic_name');
        $info_name = empty(request()->post('info_name')) ? '':request()->post('info_name');
        $project = empty(request()->post('project')) ? '':request()->post('project');
        $status = empty(request()->post('status')) ? '':request()->post('status');
        $create_begin = empty(request()->post('create_begin')) ? '':request()->post('create_begin');
        $create_end = empty(request()->post('create_end')) ? '':request()->post('create_end');

        $begin_time = empty(request()->post('begin_time')) ? '':request()->post('begin_time');
        $end_time = empty(request()->post('end_time')) ? '':request()->post('end_time');


        //分页
        if ($page == 1) {
            $page_start = 0;
            $page_end = $limt-1;
        } else {
            $page_start = ($page - 1) * $limt;
            $page_end = $page_start + $limt-1;
        }

        $where = [];
        $wheres = [];
        $wheress = [];
        if (!empty($code)){
            $where['a.code'] = ['like','%'.$code.'%'];
        }

        if (!empty($medic_name)){
            $where['c.name'] = ['like','%'.$medic_name.'%'];
        }

        if (!empty($info_name)){
            $where['b.name'] = ['like','%'.$info_name.'%'];
        }

        if (!empty($project)){
            $where['a.project'] = ['like','%'.$project.'%'];
        }

        if (!empty($status)){
            $where['a.status'] = $status;
        }

        if (!empty($medic_id)){
            $where['a.medic_id'] = $medic_id;
        }

        if (!empty($begin_time)){
            $where['a.begin_time'] = ['>=',strtotime($begin_time)];
        }

        if (!empty($end_time)){
            $where['a.end_time'] = ['<=',strtotime($end_time)];
        }

        if ((!empty($create_begin)) && (!empty($create_end))){
            $wheres['a.create_time'] = ['>=',strtotime($create_begin)];
            $wheress['a.create_time'] = ['<=',strtotime($create_end)];
        }


        $data = Db::table('fa_registration')
            ->alias('a')
            ->field('a.id,a.code,b.name,a.visiting_time,b.mobile,a.project,c.name as names,a.create_time,a.status,a.msg,a.medic_id,a.info_id')
            ->join('fa_info b','a.info_id=b.id','left')
            ->join('fa_medic c','a.medic_id=c.id','left')
            ->where($where)
            ->where($wheres)
            ->where($wheress)
            ->limit($page_start,$limt)
            ->order('id','desc')
            ->select();


        $total = Db::table('fa_registration')
            ->alias('a')
            ->join('fa_info b','a.info_id=b.id','left')
            ->join('fa_medic c','a.medic_id=c.id','left')
            ->where($where)
            ->where($wheres)
            ->where($wheress)
            ->count();

        if (!empty($data)){
            foreach ($data as $k=>$v){
                $data[$k]['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
                if ($v['status'] == '待就诊'){
                    $data[$k]['status'] = '待咨询';
                }
            }
        }


        //组装数据
        $datas = [
            'page' => $page,
            'total' => $total,
            'limt' => $limt,
            'list' => $data,
        ];

        $this->success('成功',$datas);


    }


    /**
     *预约挂号详情
     *
     */
    public function registration_details(){
        $id = empty(request()->post('id')) ? $this->error('id不能为空'):request()->post('id');

        $data = Db::table('fa_registration')
            ->alias('a')
            ->field('a.id,a.code,a.status,a.create_time,c.name as namesm,a.project,a.visiting_time,b.name,b.sex,b.birthday,a.medic_id,a.msg')
            ->join('fa_info b','a.info_id=b.id','left')
            ->join('fa_medic c','a.medic_id=c.id','left')
            ->where('a.id',$id)
            ->find();

        if (!empty($data)){
            $data['create_time'] = date('Y-m-d H:i:s',$data['create_time']);
            $data['age'] = countage($data['birthday']);
            unset($data['birthday']);

            if ($data['status'] == '待就诊'){
                $data['status'] = '待咨询';
            }
        }

        $this->success('成功',$data);

    }

    /**
     * 编辑挂号订单表
     *
     */
    public function edit_registration(){
        $id = empty(request()->post('id')) ? $this->error('id不能为空'):request()->post('id');
        $time = empty(request()->post('time')) ? $this->error('time不能为空'):request()->post('time');
        $msg = empty(request()->post('msg')) ? $this->error('msg不能为空'):request()->post('msg');
        $date = empty(request()->post('date')) ? $this->error('date不能为空'):request()->post('date');
        $medic_id = empty(request()->post('medic_id')) ? $this->error('medic_id不能为空'):request()->post('medic_id');

        $order_arr = Db::table('fa_registration')
            ->where('id',$id)
            ->find();

        if (empty($order_arr)){
            $this->error('订单数据为空',[]);
        }

        if (!in_array($time,[1,2,3,4,5,6])){
            $this->error('time参数错误',[]);
        }

        if ($date < date('Y-m-d')){
            $this->error('选择时间不能小于当前时间',[]);
        }

        $order_arr['date'] = $date;

        $pan = Db::table('fa_registration')
            ->alias('a')
            ->join('fa_working b','a.working_id=b.id','left')
            ->where(['b.date'=>$order_arr['date'],'b.time'=>$time,'b.medic_id'=>$medic_id])
            ->find();

        if (!empty($pan)){
            $this->error('该时间段已被预约',[]);
        }


        switch ($time){
            case 1:
                if (date('Y-m-d H:i:s') > $order_arr['date'].' 10:00:00'){
                    $this->error('预约时间已超时');
                }
                break;
            case 2:
                if (date('Y-m-d H:i:s') > $order_arr['date'].' 11:00:00'){
                    $this->error('预约时间已超时');
                }
                break;
            case 3:
                if (date('Y-m-d H:i:s') > $order_arr['date'].' 12:00:00'){
                    $this->error('预约时间已超时');
                }
                break;
            case 4:
                if (date('Y-m-d H:i:s') > $order_arr['date'].' 15:00:00'){
                    $this->error('预约时间已超时');
                }
                break;
            case 5:
                if (date('Y-m-d H:i:s') > $order_arr['date'].' 16:00:00'){
                    $this->error('预约时间已超时');
                }
                break;
            case 6:
                if (date('Y-m-d H:i:s') > $order_arr['date'].' 17:00:00'){
                    $this->error('预约时间已超时');
                }
                break;
            default:
                $this->error('预约时间错误');
                break;
        }



        //订单支付
        if (empty($order_arr)){
            $visiting_time = '';
            $datess = '';
        }else{
            $date = date("w",strtotime($order_arr['date']));

            switch ($time){
                case 1:
                    $tims = '09:00-10:00';
                    break;
                case 2:
                    $tims = '10:00-11:00';
                    break;
                case 3:
                    $tims = '11:00-12:00';
                    break;
                case 4:
                    $tims = '14:00-15:00';
                    break;
                case 5:
                    $tims = '15:00-16:00';
                    break;
                case 6:
                    $tims = '16:00-17:00';
                    break;
            }

            switch ($date){
                case 0:
                    $day = '(星期天)';
                    break;
                case 1:
                    $day = '(星期一)';
                    break;
                case 2:
                    $day = '(星期二)';
                    break;
                case 3:
                    $day = '(星期三)';
                    break;
                case 4:
                    $day = '(星期四)';
                    break;
                case 5:
                    $day = '(星期五)';
                    break;
                case 6:
                    $day = '(星期六)';
                    break;
                default:
                    $day = '';
                    break;
            }

            $timss = explode('-',$tims);

            $visiting_time = $order_arr['date'].' '.$day.' '.$tims;
            $datess = $order_arr['date'];
            $begin_time = strtotime($order_arr['date'].' '.$timss[0]);
            $end_time = strtotime($order_arr['date'].' '.$timss[1]);
        }


        //开启事务
        Db::startTrans();

        $working_id = Db::table('fa_working')
            ->insertGetId([
                //'medic_id' => $order_arr['medic_id'],
                'medic_id' => $medic_id,
                'date' => $order_arr['date'],
                'time' => $time,
                'create_time' => time(),
            ]);

        $insert = Db::table('fa_registration')
            ->where('id',$id)
            ->update([
                'medic_id' => $medic_id,
                'visiting_time' => $visiting_time,
                'begin_time' => $begin_time,
                'end_time' => $end_time,
                'status' => '待就诊',
                'update_time' =>time(),
                'working_id' =>$working_id,
                'msg' =>$msg,
                'date' =>$order_arr['date'],
            ]);

        if (($working_id > 0) && ($insert !== false)){
            //提交事务
            Db::commit();

            $info_arr = Db::table('fa_info')
                ->where('id',$order_arr['info_id'])
                ->find();

            if (empty($info_arr)){
                $this->success('成功,就诊人数据为空',['']);
            }

            //$sms = send_sms('SMS_21070800025',$info_arr['mobile'],['time'=>$visiting_time]);
            $sms = send_sms('SMS_21090100012',$info_arr['mobile'],['time'=>$visiting_time]);
            //$sms= 1;


            if ($sms != 1){
                //$this->error('发送失败',[]);
                $this->success('成功,短信推送失败',[]);
            }else{
                //$this->success('发送成功',[]);
                $this->success('成功',[]);
            }

            $this->success('成功',[]);

        }else{
            //事务回滚
            Db::rollback();
            $this->error('失败',[]);
        }




    }

    /**
     * 知情同意书内容
     *
     */
    public function informed_consent(){
        $id = empty(request()->post('id')) ? $this->error('id不能为空'):request()->post('id');

        $order_arr = Db::table('fa_diagnosis_order')
            ->where('id',$id)
            ->find();

        if (empty($order_arr)){
            $this->error('诊疗服务单数据为空',[]);
        }

        $ids = explode(',',$order_arr['disease_ids']);

        $data = [];
        $new = [];
        $data['disease_name'] = $order_arr['disease_name'];
        if (!empty($ids)){
            foreach ($ids as $k=>$v){
                $new[$k] = Db::table('fa_disease')
                    ->field('id,name,content')
                    ->where('id',$v)
                    ->find();
            }

            $data['arr'] = $new;
        }

        $this->success('成功',$data);

    }

    /**
     *
     * 修改日期
     */
    public function alter_date(){
        $id = empty(request()->post('id')) ? $this->error('id不能为空'):request()->post('id');
        $date = empty(request()->post('date')) ? $this->error('date不能为空'):request()->post('date');

        $order_arr = Db::table('fa_treatment')
            ->where('id',$id)
            ->find();

        if (empty($order_arr)){
            $this->error('疗程表数据为空',[]);
        }

        if ($date < date('Y-m-d')){
            $this->error('疗程日期不能小于现在时间',[]);
        }

        $update = Db::table('fa_treatment')
            ->where('id',$id)
            ->update([
                'date' => $date,
                'update_time' => time(),
            ]);

        if ($update !== false){
            $this->success('成功',[]);
        }else{
            $this->error('失败',[]);
        }


    }

    /**
     * 签字
     *
     */
    public function signature(){
        $type = empty(request()->post('type')) ? $this->error('type不能为空'):request()->post('type');
        $id = empty(request()->post('id')) ? $this->error('id不能为空'):request()->post('id');
        $img = empty(request()->post('img')) ? $this->error('img不能为空'):request()->post('img');

        $order_arr = Db::table('fa_treatment')
            ->where('id',$id)
            ->find();

        if (empty($order_arr)){
            $this->error('疗程表数据为空',[]);
        }

        $diagnosis_order = Db::table('fa_diagnosis_order')
            ->where('id',$order_arr['order_id'])
            ->find();

        if (empty($diagnosis_order)){
            $this->error('诊疗数据为空',[]);
        }

        //开启事务
        Db::startTrans();
        //1代表知情同意书签字,2代表诊后注意事项签字
        if ($type == 1){
            if (empty($order_arr['img'])){
                $finish = 0;
            }else{
                $finish = 1;
            }

            $update = Db::table('fa_treatment')
                ->where('id',$id)
                ->update([
                    'salesman_id' => $this->user_arr['id'],
                    'salesman_name' => $this->user_arr['nickname'],
                    'imgs' => $img,
                    'finish' => $finish,
                    'update_time' => time(),
                ]);

        }elseif ($type == 2){
            if (empty($order_arr['imgs'])){
                $finish = 0;
            }else{
                $finish = 1;
            }

            $update = Db::table('fa_treatment')
                ->where('id',$id)
                ->update([
                    'salesman_id' => $this->user_arr['id'],
                    'salesman_name' => $this->user_arr['nickname'],
                    'img' => $img,
                    'finish' => $finish,
                    'update_time' => time(),
                ]);
        }else{
            $this->error('type参数错误',[]);
        }

        $insert_check = 1;
        if($finish == 1){
            $count = Db::table('fa_treatment')
                ->where('order_id',$diagnosis_order['id'])
                ->count();

            $counts = Db::table('fa_treatment')
                ->where(['order_id'=>$diagnosis_order['id'],'finish'=>1])
                ->count();

            $receivable = number_format($diagnosis_order['count_cost']+$diagnosis_order['other_cost']-$diagnosis_order['vip_discounts']-$diagnosis_order['iscounts_cost']-$diagnosis_order['vip_deduction']+($diagnosis_order['new_price']),2,'.','');

            $moneyss = floor(($receivable/$count)*100)/100;

            $insert_check = Db::table('fa_check')
                ->insertGetId([
                    'order_id' => $diagnosis_order['id'],
                    'treatment_id' => $id,
                    'info_id' => $diagnosis_order['info_id'],
                    'money' => $receivable,
                    'moneys' => $moneyss,
                    'create_time' => time(),
                    'count' => $counts.'/'.$count,
                ]);
        }



        //添加健康顾问
        $salesman_name = '';
        $salesman_id = '';
        if (empty($diagnosis_order['salesman_name'])){
            $salesman_name = $this->user_arr['nickname'];
        }else{
            $salesman_name = $diagnosis_order['salesman_name'].','.$this->user_arr['nickname'];

            $eth_name = explode(',',$salesman_name);
            $eth_name = array_unique($eth_name);
            $salesman_name = implode(',',$eth_name);
        }

        if (empty($diagnosis_order['salesman_id'])){
            $salesman_id = $this->user_arr['id'];
        }else{
            $salesman_id = $diagnosis_order['salesman_id'].','.$this->user_arr['id'];

            $eth_name = explode(',',$salesman_id);
            $eth_name = array_unique($eth_name);
            $salesman_id = implode(',',$eth_name);
        }


        $updates = Db::table('fa_diagnosis_order')
            ->where('id',$diagnosis_order['id'])
            ->update([
                'salesman_name' => $salesman_name,
                'salesman_id' => $salesman_id,
                'update_time' => time(),
            ]);

        if (($update !== false) && ($updates !== false) && ($insert_check !== false)){
            //事务提交
            Db::commit();
            $this->success('成功',[]);
        }else{
            //事务回滚
            Db::rollback();
            $this->error('失败',[]);
        }


    }

    /**
     * 新增生活信息
     *
     */
    public function add_life(){
        $id = empty(request()->post('id')) ? $this->error('id不能为空'):request()->post('id');
        $smoke = empty(request()->post('smoke')) ? $this->error('smoke不能为空'):request()->post('smoke');
        $drink = empty(request()->post('drink')) ? $this->error('drink不能为空'):request()->post('drink');
        $sports = empty(request()->post('sports')) ? $this->error('sports不能为空'):request()->post('sports');
        $diet = empty(request()->post('diet')) ? $this->error('diet不能为空'):request()->post('diet');
        $sleep = empty(request()->post('sleep')) ? $this->error('sleep不能为空'):request()->post('sleep');
        $pee = empty(request()->post('pee')) ? $this->error('pee不能为空'):request()->post('pee');
        $excrement = empty(request()->post('excrement')) ? $this->error('excrement不能为空'):request()->post('excrement');
        $menstruation = empty(request()->post('menstruation')) ? '':request()->post('menstruation');
        $give_birth = empty(request()->post('give_birth')) ? '':request()->post('give_birth');

        $code = get_strings(8,2);

        $insert = Db::table('fa_life')
            ->insertGetId([
                'info_id' => $id,
                'code' => $code,
                'salesman_id' => $this->user_arr['id'],
                'salesman_name' => $this->user_arr['nickname'],
                'smoke' => $smoke,
                'drink' => $drink,
                'sports' => $sports,
                'diet' => $diet,
                'sleep' => $sleep,
                'pee' => $pee,
                'excrement' => $excrement,
                'menstruation' => $menstruation,
                'give_birth' => $give_birth,
                'create_time' => time(),
            ]);

        if ($insert > 0){
            $this->success('成功',[]);
        }else{
            $this->error('失败',[]);
        }

    }


    /**
     *查看-生活信息
     *
     */
    public function get_life(){
        $id = empty(request()->post('id')) ? $this->error('id不能为空'):request()->post('id');

        $data = Db::table('fa_life')
            ->where('info_id',$id)
            ->order('id','desc')
            ->select();

        if (!empty($data)){
            foreach ($data as $k=>$v){
                $data[$k]['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
            }
        }

        $this->success('查询成功',$data);

    }

    /**
     * 业务员工作台
     *
     */
    public function get_work(){
        $begin_month = date('Y-m-01', strtotime(date("Y-m-d")));
        $end_month = date('Y-m-d', strtotime("$begin_month +1 month -1 day"));

        $three_day = date('Y-m-d', strtotime(" +2 day"));
        $seven_day = date('Y-m-d', strtotime(" +6 day"));

        $Monday = date("Y-m-d",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y")));
        $Sunday = date("Y-m-d",mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y")));


        $Mondays = date("Y-m-d",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1-7,date("Y")));
        $Sundays = date("Y-m-d",mktime(23,59,59,date("m"),date("d")-date("w")+7-7,date("Y")));

        $month_begin = date("Y-m-d",mktime(0, 0 , 0,date("m"),1,date("Y")));
        $month_end = date("Y-m-d",mktime(23,59,59,date("m"),date("t"),date("Y")));


        //预约上午人数
        $forenoon = Db::table('fa_registration')
            ->alias('a')
            ->join('fa_working b','a.working_id=b.id','left')
            ->where(['b.time'=>['<=',3],'a.status'=>'待就诊','a.date'=>date('Y-m-d')])
            ->count();

        //预约下午人数
        $afternoon = Db::table('fa_registration')
            ->alias('a')
            ->join('fa_working b','a.working_id=b.id','left')
            ->where(['b.time'=>['>=',3],'a.status'=>'待就诊','a.date'=>date('Y-m-d')])
            ->count();

        //预约今日未到诊人数
        $arrival = Db::table('fa_registration')
            ->alias('a')
            ->join('fa_working b','a.working_id=b.id','left')
            ->where(['a.status'=>'待就诊','a.end_time'=>['<',time()]])
            ->where('a.begin_time','>',strtotime(date('Y-m-d')))
            ->count();


        //今天诊疗疗程人数
        $today = Db::table('fa_treatment')
            ->alias('a')
            ->join('fa_diagnosis_order b','a.order_id=b.id','left')
            ->where(['a.date'=>date('Y-m-d')])
            ->count();

        //近3天诊疗疗程人数
        $three = Db::table('fa_treatment')
            ->alias('a')
            ->join('fa_diagnosis_order b','a.order_id=b.id','left')
            ->where(['a.date'=>['between time',[date('Y-m-d'),$three_day]]])
            ->group('b.id')
            ->count();

        //近7天诊疗疗程人数
        $seven = Db::table('fa_treatment')
            ->alias('a')
            ->join('fa_diagnosis_order b','a.order_id=b.id','left')
            ->where(['a.date'=>['between time',[date('Y-m-d'),$seven_day]]])
            ->group('b.id')
            ->count();


        //本周就诊人数
        $week = Db::table('fa_diagnosis_order')
            ->where(['create_time'=>['>=',strtotime($Monday)]])
            ->where(['create_time'=>['<=',strtotime($Sunday)]])
            ->count();

        //上周就诊人数
        $last_week = Db::table('fa_diagnosis_order')
            ->where(['create_time'=>['<=',strtotime($Sundays)]])
            ->where(['create_time'=>['>=',strtotime($Mondays)]])
            ->count();

        //本月就诊人数
        $month = Db::table('fa_diagnosis_order')
            ->where(['create_time'=>['<=',strtotime($month_end)]])
            ->where(['create_time'=>['>=',strtotime($month_begin)]])
            ->count();

        $data = [
            'forenoon' => $forenoon,
            'forenoon_begin' => date('Y-m-d').' 09:00:00',
            'forenoon_end' => date('Y-m-d').' 12:00:00',
            'afternoon' => $afternoon,
            'afternoon_begin' => date('Y-m-d').' 12:00:00',
            'afternoon_end' => date('Y-m-d').' 18:00:00',
            'arrival' => $arrival,
            'arrival_begin' => date('Y-m-d').' 00:00:00',
            'arrival_end' => date('Y-m-d H:i:s'),
            'today' => $today,
            'today_time' => date('Y-m-d'),
            'three' => $three,
            'three_begin' => date('Y-m-d'),
            'three_end' => $three_day,
            'seven' => $seven,
            'seven_begin' => date('Y-m-d'),
            'seven_end' => $seven_day,
            'week' => $week,
            'week_begin' => $Monday,
            'week_end' => $Sunday,
            'last_week' => $last_week,
            'last_begin' => $Mondays,
            'last_end' => $Sundays,
            'month' => $month,
            'month_begin' => $month_begin,
            'month_end' => $month_end,
        ];

        $this->success('查询成功',$data);

    }


    /**
     * 获取发送人
     *
     */
    public function admin_list(){
        $data = Db::table('fa_admin')
            ->field('id,nickname,avatar')
            ->where('status','normal')
            ->where('id','<>',$this->user_arr['id'])
            ->select();

        $this->success('查询成功',$data);
    }
    /**
     * 新增日志
     *
     */
    public function add_log(){
        $content = empty(request()->post('content')) ? $this->error('content不能为空'):request()->post('content');
        $images = empty(request()->post('images')) ? '':request()->post('images');
        $ids = empty(request()->post('ids')) ? $this->error('ids不能为空'):request()->post('ids');


        $ids_arr = explode(',',$ids);

        if (count($ids_arr) != count(array_unique($ids_arr))) {
            $this->error(__('抄送人不能重复'));
        }

        //开启事务
        Db::startTrans();

        $insert = Db::table('fa_oa_log')
            ->insertGetId([
                'admin_id' => $this->user_arr['id'],
                'content' => $content,
                'images' => $images,
                'create_time' => time(),
            ]);

        $id = $insert;//新增的id
        $arr = [];
        if (!empty($ids_arr)){
            foreach ($ids_arr as $k=>$v){
                $arr[$k] = [
                    'log_id' => $id,
                    'admin_id' => $v,
                    'create_time' => time(),
                    'is_comment' => 0,
                    'status' => 1,
                ];
            }

            $inserts = Db::table('fa_oa_relation')
                ->insertAll($arr);
        }else{
            $inserts = 1;
        }


        if (($insert !== false) && ($inserts) !== false){
            //事务提交
            Db::commit();
            $this->success('成功');
        }else{
            //事务回滚
            Db::rollback();
            $this->error('失败');
        }

    }


    /**
     * 日志列表接口
     *
     */
    public function log_list(){
        $type = empty(request()->post('type')) ? $this->error('type不能为空'):request()->post('type');

        $page = empty(request()->post('page')) ? 1:request()->post('page');//获取页码
        $limt = empty(request()->post('limt')) ? 10:request()->post('limt');//获取每页显示条数

        $begin_time = empty(request()->post('begin_time')) ? '':request()->post('begin_time');
        $end_time = empty(request()->post('end_time')) ? '':request()->post('end_time');

        $ids = empty(request()->post('ids')) ? '':request()->post('ids');

        //分页
        if ($page == 1) {
            $page_start = 0;
            $page_end = $limt-1;
        } else {
            $page_start = ($page - 1) * $limt;
            $page_end = $page_start + $limt-1;
        }

        $where = [];

        if ($type == 1){

            if (!empty($begin_time)){
                $where['a.create_time'] = ['>=',strtotime($begin_time)];
            }

            if (!empty($end_time)){
                $where['a.create_time'] = ['<=',strtotime($end_time)];
            }

            $data = Db::table('fa_oa_log')
                ->alias('a')
                ->field('a.id,b.nickname,b.avatar,a.create_time')
                ->join('fa_admin b','a.admin_id=b.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->where($where)
                ->limit($page_start,$limt)
                ->order('id','desc')
                ->select();


            $total = Db::table('fa_oa_log')
                ->alias('a')
                ->join('fa_admin b','a.admin_id=b.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->where($where)
                ->count();
        }elseif ($type == 2){
            if (!empty($ids)){
                $where['b.admin_id'] = ['in',$ids];
            }

            if (!empty($begin_time)){
                $where['b.create_time'] = ['>=',strtotime($begin_time)];
            }

            if (!empty($end_time)){
                $where['b.create_time'] = ['<=',strtotime($end_time)];
            }


            $data = Db::table('fa_oa_relation')
                ->alias('a')
                ->field('b.id,c.nickname,c.avatar,b.create_time')
                ->join('fa_oa_log b','a.log_id=b.id','left')
                ->join('fa_admin c','b.admin_id=c.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->where('a.status',1)
                ->where($where)
                ->limit($page_start,$limt)
                ->order('id','desc')
                ->select();

            $total = Db::table('fa_oa_relation')
                ->alias('a')
                ->join('fa_oa_log b','a.log_id=b.id','left')
                ->join('fa_admin c','b.admin_id=c.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->where('a.status',1)
                ->where($where)
                ->count();
        }


        if (!empty($data)){
            foreach ($data as $k=>$v){
                $data[$k]['nickname'] = $v['nickname'].'的日志';
                $data[$k]['create_time'] = gew($v['create_time']);
            }
        }


        //组装数据
        $datas = [
            'page' => $page,
            'total' => $total,
            'limt' => $limt,
            'list' => $data,
        ];

        $this->success('成功',$datas);

    }

    /**
     * 日志详情
     *
     */
    public function log_details(){
        $id = empty(request()->post('id')) ? $this->error('id不能为空'):request()->post('id');

        $data = Db::table('fa_oa_log')
            ->alias('a')
            ->field('a.id,b.nickname,b.avatar,a.content,a.images,a.create_time')
            ->join('fa_admin b','a.admin_id=b.id','left')
            ->where('a.id',$id)
            ->find();

        if (!empty($data)){
            $data['nickname'] = $data['nickname'].'的日志';
            $data['create_time'] = gew($data['create_time']);

            if (!empty($data['images'])){
                $data['images'] = explode(',',$data['images']);
            }else{
                $data['images'] = [];
            }

        }

        $this->success('成功',$data);
    }

    /**
     * 评论
     *
     */
    public function send_comment(){
        $id = empty(request()->post('id')) ? $this->error('id不能为空'):request()->post('id');
        $msg = empty(request()->post('msg')) ? $this->error('msg不能为空'):request()->post('msg');

        $pan_arr = Db::table('fa_oa_relation')
            ->where(['log_id'=>$id,'admin_id'=>$this->user_arr['id'],'status'=>1])
            ->find();

        if (empty($pan_arr)){
            $this->error('关系数据为空');
        }

        if ($pan_arr['is_comment'] == 1){
            $this->error('已评论过，请勿重复评论');
        }


        //开启事务
        Db::startTrans();

        $insert = Db::table('fa_oa_comment')
            ->insertGetId([
                'admin_id' => $this->user_arr['id'],
                'log_id' => $id,
                'msg' => $msg,
                'create_time' => time(),
            ]);

        $update = Db::table('fa_oa_relation')
            ->where('id',$pan_arr['id'])
            ->update([
                'is_comment' => 1,
                'update_time' => time(),
            ]);

        if (($insert !== false) && ($update !== false)){
            //事务提交
            Db::commit();
            $this->success('成功');
        }else{
            //事务回滚
            Db::rollback();
            $this->error('失败');
        }
    }


    /**
     * 评论列表
     *
     */
    public function comment_list(){
        $id = empty(request()->post('id')) ? $this->error('id不能为空'):request()->post('id');
        //评论数据
        $comment = Db::table('fa_oa_comment')
            ->alias('a')
            ->field('a.id,b.nickname,b.avatar,a.create_time,a.msg')
            ->join('fa_admin b','a.admin_id=b.id','left')
            ->where('a.log_id',$id)
            ->order('id','desc')
            ->select();

        if (!empty($comment)){
            foreach ($comment as $k=>$v){
                $comment[$k]['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
            }
        }
        $this->success('成功',$comment);
    }

    /**
     * 新增-物品申请
     *
     */
    public function add_goods(){
        $purpose = empty(request()->post('purpose')) ? $this->error('purpose不能为空'):request()->post('purpose');
        $content = empty(request()->post('content')) ? $this->error('content不能为空'):request()->post('content');
        $images = empty(request()->post('images')) ? '':request()->post('images');

        $detail = empty(input('post.detail/a')) ? $this->error('detail不能为空'):input('post.detail/a');
        $ids = empty(request()->post('ids')) ? '':request()->post('ids');

        $pan_count = explode(',',$images);
        if (count($pan_count) > 9){
            $this->error(__('数量最大添加9张'));
        }

        if (!empty($ids)){
            $ids_arr = explode(',',$ids);

            if (count($ids_arr) != count(array_unique($ids_arr))) {
                $this->error(__('抄送人不能重复'));
            }
        }

        $examine = Db::table('fa_oa_examine')
            ->where(['oa_id'=>1])
            ->order('type','asc')
            ->select();

        if (empty($examine)){
            $this->error(__('审批人为空,请去OA流转设置'));
        }

        $str_arr = [];
        foreach ($examine as $k=>$v){
            $str_arr[$k] = $v['admin_id'];
        }

        $string = implode(',', $str_arr);

        //开启事务
        Db::startTrans();

        $insert = Db::table('fa_oa_goods')
            ->insertGetId([
                'admin_id' => $this->user_arr['id'],
                'purpose' => $purpose,
                'content' => $content,
                'images' => $images,
                'create_time' => time(),
                'ids' => $string,
            ]);

        $id = $insert;//新增的id


        $details = [];

        foreach ($detail as $k=>$v){
            if (empty($v['name'])){
                //事务回滚
                Db::rollback();
                $this->error(__('物品名称不能为空'));
            }

            if (empty($v['count'])){
                //事务回滚
                Db::rollback();
                $this->error(__('物品数量不能为空'));
            }

            $details[$k] = [
                'oa_id' => $id,
                'name' => $v['name'],
                'count' => $v['count'],
                'create_time' => time(),
            ];

        }
        //新增物品明细
        $insertall = Db::table('fa_oa_detail')
            ->insertAll($details);


        $arr = [];
        if (!empty($ids)){
            foreach ($ids_arr as $k=>$v){
                $arr[$k] = [
                    'log_id' => $id,
                    'admin_id' => $v,
                    'create_time' => time(),
                    'is_comment' => 0,
                    'status' => 2,
                    'type' => 1,
                ];
            }

            $inserts = Db::table('fa_oa_relation')
                ->insertAll($arr);
        }else{
            $inserts = 1;
        }


        $insertss = Db::table('fa_oa_relation')
            ->insertGetId([
                'log_id' => $id,
                'admin_id' => $examine[0]['admin_id'],
                'create_time' => time(),
                'is_comment' => 0,
                'status' => 2,
                'type' => 0,
            ]);

        if (($insert !== false) && ($insertall !== false) && ($inserts !== false) && ($insertss !== false)){
            //事务提交
            Db::commit();
            $this->success('成功');
        }else{
            //事务回滚
            Db::rollback();
            $this->error('失败');
        }


    }


    /**
     * 物品申请列表
     *
     */
    public function goods_list(){
        $type = empty(request()->post('type')) ? $this->error('type不能为空'):request()->post('type');

        $page = empty(request()->post('page')) ? 1:request()->post('page');//获取页码
        $limt = empty(request()->post('limt')) ? 10:request()->post('limt');//获取每页显示条数

        //分页
        if ($page == 1) {
            $page_start = 0;
            $page_end = $limt-1;
        } else {
            $page_start = ($page - 1) * $limt;
            $page_end = $page_start + $limt-1;
        }


        if ($type == 1){
            $data = Db::table('fa_oa_goods')
                ->alias('a')
                ->field('a.id,b.nickname,b.avatar,a.create_time,a.status')
                ->join('fa_admin b','a.admin_id=b.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->limit($page_start,$limt)
                ->order('id','desc')
                ->select();

            $total = Db::table('fa_oa_goods')
                ->alias('a')
                ->join('fa_admin b','a.admin_id=b.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->count();
        }elseif ($type == 2){

            $data = Db::table('fa_oa_relation')
                ->alias('a')
                ->field('b.id,c.nickname,c.avatar,b.create_time,b.status')
                ->join('fa_oa_goods b','a.log_id=b.id','left')
                ->join('fa_admin c','b.admin_id=c.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->where('a.status',2)
                ->where('a.type',0)
                ->limit($page_start,$limt)
                ->order('id','desc')
                ->select();

            $total = Db::table('fa_oa_relation')
                ->alias('a')
                ->join('fa_oa_goods b','a.log_id=b.id','left')
                ->join('fa_admin c','b.admin_id=c.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->where('a.status',2)
                ->where('a.type',0)
                ->count();
        }elseif ($type == 3){
            $data = Db::table('fa_oa_relation')
                ->alias('a')
                ->field('b.id,c.nickname,c.avatar,b.create_time,b.status')
                ->join('fa_oa_goods b','a.log_id=b.id','left')
                ->join('fa_admin c','b.admin_id=c.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->where('a.status',2)
                ->where('a.type',1)
                ->limit($page_start,$limt)
                ->order('id','desc')
                ->select();

            $total = Db::table('fa_oa_relation')
                ->alias('a')
                ->join('fa_oa_goods b','a.log_id=b.id','left')
                ->join('fa_admin c','b.admin_id=c.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->where('a.status',2)
                ->where('a.type',1)
                ->count();
        }


        if (!empty($data)){
            foreach ($data as $k=>$v){
                $data[$k]['nickname'] = $v['nickname'].'的物品申请';
                $data[$k]['create_time'] = gew($v['create_time']);

                if ($v['status'] == 0){
                    $data[$k]['status'] = '待审核';
                }elseif ($v['status'] == 1){
                    $data[$k]['status'] = '审核通过';
                }elseif ($v['status'] == 2){
                    $data[$k]['status'] = '审核驳回';
                }else{
                    $data[$k]['status'] = '';
                }
            }
        }


        //组装数据
        $datas = [
            'page' => $page,
            'total' => $total,
            'limt' => $limt,
            'list' => $data,
        ];

        $this->success('成功',$datas);
    }

    /**
     * 物品申请详情
     *
     */
    public function goods_detail(){
        $id = empty(request()->post('id')) ? $this->error('id不能为空'):request()->post('id');

        $data = Db::table('fa_oa_goods')
            ->field('id,admin_id,purpose,content,images,create_time,ids')
            ->where('id',$id)
            ->find();

        if (!empty($data)){
            $data['create_time'] = date('Y-m-d H:i:s',$data['create_time']);

            if (!empty($data['images'])){
                $data['images'] = explode(',',$data['images']);
            }else{
                $data['images'] = [];
            }

            $data['detail'] = Db::table('fa_oa_detail')
                ->field('id,name,count')
                ->where('oa_id',$data['id'])
                ->select();

            $data['send'] = Db::table('fa_oa_relation')
                ->alias('a')
                ->field('a.id,b.nickname,b.avatar')
                ->join('fa_admin b','a.admin_id=b.id','left')
                ->where(['a.log_id'=>$data['id'],'a.status'=>2,'a.type'=>1])
                ->select();

            $arr = Db::table('fa_admin')
                ->field('id,nickname,avatar')
                ->where('id','in',$data['ids'])
                ->select();

            $data['status'] = '';
            $arrs = [];
            foreach ($arr as $k=>$v){
                $pan = Db::table('fa_oa_relation')
                    ->where(['log_id'=>$data['id'],'admin_id'=>$v['id'],'status'=>2,'type'=>0])
                    ->find();

                if (empty($pan)){
                    $status = '';
                    $msg = '';
                    $create_time = '';
                }else{

                    if ($pan['is_comment'] == 0){
                        $status = '待审核';
                    }elseif ($pan['is_comment'] == 1){
                        $status = '审核通过';
                    }elseif ($pan['is_comment'] == 2){
                        $status = '审核驳回';
                    }else{
                        $status = '无';
                    }

                    if ($v['id'] == $this->user_arr['id']){
                        $data['status'] = $pan['is_comment'];
                    }


                    $msg = $pan['msg'];
                    if (empty($pan['update_time'])){
                        $create_time = '';
                    }else{
                        $create_time = date('Y-m-d H:i:s',$pan['update_time']);
                    }

                }

                $arrs[$k] = [
                    'nickname' => $v['nickname'],
                    'avatar' => $v['avatar'],
                    'create_time' => $create_time,
                    'status' => $status,
                    'msg' => $msg,
                ];
            }

            $adim_arr = Db::table('fa_admin')
                ->where('id',$data['admin_id'])
                ->find();


            $arr_z[0] = [
                'nickname' => $adim_arr['nickname'],
                'avatar' => $adim_arr['avatar'],
                'create_time' => $data['create_time'],
                'status' => '发起申请',
                'msg' => '',
            ];

            $arrss = array_merge($arr_z,$arrs);

            $data['examine'] = $arrss;
            unset($data['ids']);

        }

        $this->success('成功',$data);

    }

    /**
     * 审批
     *
     */
    public function examine(){
        $id = empty(request()->post('id')) ? $this->error('id不能为空'):request()->post('id');
        $status = empty(request()->post('status')) ? $this->error('status不能为空'):request()->post('status');
        $msg = empty(request()->post('msg')) ? '':request()->post('msg');

        $type = empty(request()->post('type')) ? $this->error('type不能为空'):request()->post('type');

        switch ($type){
            case 2:
                $status_a = $type;
                $db = 'fa_oa_goods';
                break;
            case 3:
                $status_a = $type;
                $db = 'fa_oa_leave';
                break;
            case 4:
                $status_a = $type;
                $db = 'fa_oa_evection';
                break;
            case 5:
                $status_a = $type;
                $db = 'fa_oa_universal';
                break;
            case 6:
                $status_a = $type;
                $db = 'fa_oa_apply';
                break;
            case 9:
                $status_a = $type;
                $db = 'fa_oa_buy';
                break;
            case 10:
                $status_a = $type;
                $db = 'fa_oa_give';
                break;
            case 11:
                $status_a = $type;
                $db = 'fa_oa_client';
                break;
            case 12:
                $status_a = $type;
                $db = 'fa_oa_stamp';
                break;
            default:
                $this->error(__('type参数错误'));
                break;
        }

        $oa_arr = Db::table($db)
            ->where('id',$id)
            ->find();

        if (empty($oa_arr)){
            $this->error(__('数据为空'));
        }

        if (empty($oa_arr['ids'])){
            $this->error(__('审核人数据为空'));
        }

        $pan = Db::table('fa_oa_relation')
            ->where(['log_id'=>$id,'admin_id'=>$this->user_arr['id'],'status'=>$status_a,'type'=>0])
            ->find();

        if (empty($pan)){
            $this->error(__('审批数据为空'));
        }

        if (!empty($pan['update_time'])){
            $this->error(__('已审批过，请勿重复操作'));
        }

        //开启事务
        Db::startTrans();

        $update = Db::table('fa_oa_relation')
            ->where('id',$pan['id'])
            ->update([
                'is_comment' => $status,
                'msg' => $msg,
                'update_time' => time(),
            ]);

        if ($status == 1){
            $pan_arr = [];
            $pan_arrs = Db::table('fa_admin')
                ->field('id')
                ->where('id','in',$oa_arr['ids'])
                ->select();


            foreach ($pan_arrs as $k=>$v){
                $pan_arr[$k]['type'] = $k+1;
                $pan_arr[$k]['admin_id'] = $v['id'];
            }

            $count = count($pan_arr);

            if ($count == 0){
                //事务回滚
                Db::rollback();
                $this->error(__('OA流转设置为空'));
            }elseif ($count == 1){
                $update_goods = Db::table($db)
                    ->where('id',$id)
                    ->update([
                        'status' => 1,
                        'update_time' => time(),
                    ]);
                $updates = 1;
            }else{
                $sw = '无';

                foreach ($pan_arr as $k=>$v){
                    if ($v['admin_id'] == $this->user_arr['id']){
                        $sw = $k;
                    }
                }

                if ($sw !== '无'){
                    if($pan_arr[$sw]['type'] == $count){
                        $update_goods = Db::table($db)
                            ->where('id',$id)
                            ->update([
                                'status' => 1,
                                'update_time' => time(),
                            ]);
                        $updates = 1;
                    }else{
                        $updates = Db::table('fa_oa_relation')
                            ->insertGetId([
                                'log_id' => $id,
                                'admin_id' => $pan_arr[$sw+1]['admin_id'],
                                'create_time' => time(),
                                'status' => $status_a,
                                'type' => 0,
                            ]);
                        $update_goods = 1;
                    }

                }else{
                    $update_goods = 1;
                    $updates = 1;
                }

            }
        }elseif ($status == 2){
            $update_goods = Db::table($db)
                ->where('id',$id)
                ->update([
                    'status' => 2,
                    'update_time' => time(),
                ]);
            $updates = 1;
        }

        if (($update !== false) && ($update_goods !== false) && ($updates !== false)){
            //事务提交
            Db::commit();
            $this->success('成功');
        }else{
            //事务回滚
            Db::rollback();
            $this->error(__('失败'));
        }

    }

    /**
     * 新增-客户维护
     *
     */
    public function add_client(){
        $name = empty(request()->post('name')) ? $this->error('name不能为空'):request()->post('name');
        $phone = empty(request()->post('phone')) ? $this->error('phone不能为空'):request()->post('phone');
        $goal = empty(request()->post('goal')) ? $this->error('goal不能为空'):request()->post('goal');
        $site = empty(request()->post('site')) ? $this->error('site不能为空'):request()->post('site');

        $background = empty(request()->post('background')) ? '':request()->post('background');
        $principal = empty(request()->post('principal')) ? '':request()->post('principal');
        $post = empty(request()->post('post')) ? '':request()->post('post');
        $mobile = empty(request()->post('mobile')) ? '':request()->post('mobile');
        $agent = empty(request()->post('agent')) ? '':request()->post('agent');
        $posts = empty(request()->post('posts')) ? '':request()->post('posts');
        $mobiles = empty(request()->post('mobiles')) ? '':request()->post('mobiles');

        $ids = empty(request()->post('ids')) ? '':request()->post('ids');
        $detail = empty(input('post.detail/a')) ? $this->error('detail不能为空'):input('post.detail/a');

        $phone = is_phone($phone);
        if ($phone == false){
            $this->error(__('手机号格式不正确'));
        }

        if (!empty($ids)){
            $ids_arr = explode(',',$ids);

            if (count($ids_arr) != count(array_unique($ids_arr))) {
                $this->error(__('抄送人不能重复'));
            }
        }

        $examine = Db::table('fa_oa_examine')
            ->where(['oa_id'=>10])
            ->order('type','asc')
            ->select();

        if (empty($examine)){
            $this->error(__('审批人为空,请去OA流转设置'));
        }

        $str_arr = [];
        foreach ($examine as $k=>$v){
            $str_arr[$k] = $v['admin_id'];
        }

        $string = implode(',', $str_arr);

        //开启事务
        Db::startTrans();

        $insert = Db::table('fa_oa_client')
            ->insertGetId([
                'admin_id' => $this->user_arr['id'],
                'name' => $name,
                'phone' => $phone,
                'goal' => $goal,
                'site' => $site,
                'background' => $background,
                'principal' => $principal,
                'post' => $post,
                'mobile' => $mobile,
                'agent' => $agent,
                'posts' => $posts,
                'mobiles' => $mobiles,
                'create_time' => time(),
                'ids' => $string,
            ]);

        $id = $insert;//新增的id


        $details = [];

        foreach ($detail as $k=>$v){
            if (empty($v['time'])){
                //事务回滚
                Db::rollback();
                $this->error(__('沟通时间不能为空'));
            }

            if (empty($v['message'])){
                //事务回滚
                Db::rollback();
                $this->error(__('重要信息不能为空'));
            }

            if (empty($v['results'])){
                //事务回滚
                Db::rollback();
                $this->error(__('结果及异议不能为空'));
            }

            if (empty($v['solution'])){
                //事务回滚
                Db::rollback();
                $this->error(__('应对方案不能为空'));
            }

            if (empty($v['msg'])){
                //事务回滚
                Db::rollback();
                $this->error(__('备注不能为空'));
            }

            $details[$k] = [
                'oa_id' => $id,
                'time' => $v['time'],
                'message' => $v['message'],
                'results' => $v['results'],
                'solution' => $v['solution'],
                'msg' => $v['msg'],
                'create_time' => time(),
            ];

        }
        //新增物品明细
        $insertall = Db::table('fa_oa_record')
            ->insertAll($details);


        $arr = [];
        if (!empty($ids)){
            foreach ($ids_arr as $k=>$v){
                $arr[$k] = [
                    'log_id' => $id,
                    'admin_id' => $v,
                    'create_time' => time(),
                    'is_comment' => 0,
                    'status' => 11,
                    'type' => 1,
                ];
            }

            $inserts = Db::table('fa_oa_relation')
                ->insertAll($arr);
        }else{
            $inserts = 1;
        }


        $insertss = Db::table('fa_oa_relation')
            ->insertGetId([
                'log_id' => $id,
                'admin_id' => $examine[0]['admin_id'],
                'create_time' => time(),
                'is_comment' => 0,
                'status' => 11,
                'type' => 0,
            ]);

        if (($insert !== false) && ($insertall !== false) && ($inserts !== false) && ($insertss !== false)){
            //事务提交
            Db::commit();
            $this->success('成功');
        }else{
            //事务回滚
            Db::rollback();
            $this->error('失败');
        }


    }

    /**
     * 客户维护列表
     *
     */
    public function client_list(){
        $type = empty(request()->post('type')) ? $this->error('type不能为空'):request()->post('type');

        $name = empty(request()->post('name')) ? '':request()->post('name');//获取页码

        $page = empty(request()->post('page')) ? 1:request()->post('page');//获取页码
        $limt = empty(request()->post('limt')) ? 10:request()->post('limt');//获取每页显示条数

        //分页
        if ($page == 1) {
            $page_start = 0;
            $page_end = $limt-1;
        } else {
            $page_start = ($page - 1) * $limt;
            $page_end = $page_start + $limt-1;
        }

        $where = [];
        if ($type == 1){
            if (!empty($name)){
                $where['a.name'] = ['like','%'.$name.'%'];
            }

            $data = Db::table('fa_oa_client')
                ->alias('a')
                ->field('a.id,b.nickname,b.avatar,a.create_time,a.status,a.name,a.phone,a.goal,a.site')
                ->join('fa_admin b','a.admin_id=b.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->where($where)
                ->limit($page_start,$limt)
                ->order('id','desc')
                ->select();

            $total = Db::table('fa_oa_client')
                ->alias('a')
                ->join('fa_admin b','a.admin_id=b.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->where($where)
                ->count();
        }elseif ($type == 2){
            if (!empty($name)){
                $where['b.name'] = ['like','%'.$name.'%'];
            }

            $data = Db::table('fa_oa_relation')
                ->alias('a')
                ->field('b.id,c.nickname,c.avatar,b.create_time,b.status,b.name,b.phone,b.goal,b.site')
                ->join('fa_oa_client b','a.log_id=b.id','left')
                ->join('fa_admin c','b.admin_id=c.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->where(['a.status'=>11,'a.type'=>0])
                ->where($where)
                ->limit($page_start,$limt)
                ->order('id','desc')
                ->select();

            $total = Db::table('fa_oa_relation')
                ->alias('a')
                ->join('fa_oa_client b','a.log_id=b.id','left')
                ->join('fa_admin c','b.admin_id=c.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->where(['a.status'=>11,'a.type'=>0])
                ->where($where)
                ->count();
        }elseif ($type == 3){
            if (!empty($name)){
                $where['b.name'] = ['like','%'.$name.'%'];
            }

            $data = Db::table('fa_oa_relation')
                ->alias('a')
                ->field('b.id,c.nickname,c.avatar,b.create_time,b.status,b.name,b.phone,b.goal,b.site')
                ->join('fa_oa_client b','a.log_id=b.id','left')
                ->join('fa_admin c','b.admin_id=c.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->where(['a.status'=>11,'a.type'=>1])
                ->where($where)
                ->limit($page_start,$limt)
                ->order('id','desc')
                ->select();


            $total = Db::table('fa_oa_relation')
                ->alias('a')
                ->join('fa_oa_client b','a.log_id=b.id','left')
                ->join('fa_admin c','b.admin_id=c.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->where(['a.status'=>11,'a.type'=>1])
                ->where($where)
                ->count();
        }


        if (!empty($data)){
            foreach ($data as $k=>$v){
                $data[$k]['nickname'] = $v['nickname'].'的客户维护';
                $data[$k]['create_time'] = date('Y-m-d H:i:s',$v['create_time']);

                if ($v['status'] == 0){
                    $data[$k]['status'] = '待审核';
                }elseif ($v['status'] == 1){
                    $data[$k]['status'] = '审核通过';
                }elseif ($v['status'] == 2){
                    $data[$k]['status'] = '审核驳回';
                }else{
                    $data[$k]['status'] = '';
                }
            }
        }


        //组装数据
        $datas = [
            'page' => $page,
            'total' => $total,
            'limt' => $limt,
            'list' => $data,
        ];

        $this->success('成功',$datas);
    }

    /**
     * 客户维护详情
     *
     */
    public function client_detail(){
        $id = empty(request()->post('id')) ? $this->error('id不能为空'):request()->post('id');

        $data = Db::table('fa_oa_client')
            ->field('id,admin_id,name,phone,goal,site,background,principal,post,mobile,agent,posts,mobiles,create_time,ids')
            ->where('id',$id)
            ->find();

        if (!empty($data)){
            $data['create_time'] = date('Y-m-d H:i:s',$data['create_time']);

            $data['detail'] = Db::table('fa_oa_record')
                ->field('id,time,message,results,solution,msg')
                ->where('oa_id',$data['id'])
                ->select();

            $data['send'] = Db::table('fa_oa_relation')
                ->alias('a')
                ->field('a.id,b.nickname,b.avatar')
                ->join('fa_admin b','a.admin_id=b.id','left')
                ->where(['a.log_id'=>$data['id'],'a.status'=>11,'a.type'=>1])
                ->select();

            $arr = Db::table('fa_admin')
                ->field('id,nickname,avatar')
                ->where('id','in',$data['ids'])
                ->select();

            $data['status'] = '';
            $arrs = [];
            foreach ($arr as $k=>$v){
                $pan = Db::table('fa_oa_relation')
                    ->where(['log_id'=>$data['id'],'admin_id'=>$v['id'],'status'=>11,'type'=>0])
                    ->find();

                if (empty($pan)){
                    $status = '';
                    $msg = '';
                    $create_time = '';
                }else{

                    if ($pan['is_comment'] == 0){
                        $status = '待审核';
                    }elseif ($pan['is_comment'] == 1){
                        $status = '审核通过';
                    }elseif ($pan['is_comment'] == 2){
                        $status = '审核驳回';
                    }else{
                        $status = '无';
                    }

                    if ($v['id'] == $this->user_arr['id']){
                        $data['status'] = $pan['is_comment'];
                    }

                    $msg = $pan['msg'];
                    if (empty($pan['update_time'])){
                        $create_time = '';
                    }else{
                        $create_time = date('Y-m-d H:i:s',$pan['update_time']);
                    }

                }

                $arrs[$k] = [
                    'nickname' => $v['nickname'],
                    'avatar' => $v['avatar'],
                    'create_time' => $create_time,
                    'status' => $status,
                    'msg' => $msg,
                ];
            }

            $adim_arr = Db::table('fa_admin')
                ->where('id',$data['admin_id'])
                ->find();


            $arr_z[0] = [
                'nickname' => $adim_arr['nickname'],
                'avatar' => $adim_arr['avatar'],
                'create_time' => $data['create_time'],
                'status' => '发起申请',
                'msg' => '',
            ];

            $arrss = array_merge($arr_z,$arrs);

            $data['examine'] = $arrss;

            unset($data['ids']);
        }

        $this->success('成功',$data);

    }

    /**
     * 新增-请假
     *
     */
    public function add_leave(){
        $type = empty(request()->post('type')) ? $this->error('type不能为空'):request()->post('type');
        $start_time = empty(request()->post('start_time')) ? $this->error('start_time不能为空'):request()->post('start_time');
        $end_time = empty(request()->post('end_time')) ? $this->error('end_time不能为空'):request()->post('end_time');
        $day = empty(request()->post('day')) ? $this->error('day不能为空'):request()->post('day');
        $msg = empty(request()->post('msg')) ? $this->error('msg不能为空'):request()->post('msg');

        $images = empty(request()->post('images')) ? '':request()->post('images');

        $ids = empty(request()->post('ids')) ? '':request()->post('ids');


        $pan_count = explode(',',$images);
        if (count($pan_count) > 9){
            $this->error(__('数量最大添加9张'));
        }

        if (!empty($ids)){
            $ids_arr = explode(',',$ids);

            if (count($ids_arr) != count(array_unique($ids_arr))) {
                $this->error(__('抄送人不能重复'));
            }
        }

        $examine = Db::table('fa_oa_examine')
            ->where(['oa_id'=>2])
            ->order('type','asc')
            ->select();

        if (empty($examine)){
            $this->error(__('审批人为空,请去OA流转设置'));
        }

        $str_arr = [];
        foreach ($examine as $k=>$v){
            $str_arr[$k] = $v['admin_id'];
        }

        $string = implode(',', $str_arr);

        //开启事务
        Db::startTrans();

        $insert = Db::table('fa_oa_leave')
            ->insertGetId([
                'admin_id' => $this->user_arr['id'],
                'code' => get_strings(10,2),
                'type' => $type,
                'start_time' => $start_time,
                'end_time' => $end_time,
                'day' => $day,
                'msg' => $msg,
                'images' => $images,
                'create_time' => time(),
                'ids' => $string,
            ]);

        $id = $insert;//新增的id

        $arr = [];
        if (!empty($ids)){
            foreach ($ids_arr as $k=>$v){
                $arr[$k] = [
                    'log_id' => $id,
                    'admin_id' => $v,
                    'create_time' => time(),
                    'is_comment' => 0,
                    'status' => 3,
                    'type' => 1,
                ];
            }

            $inserts = Db::table('fa_oa_relation')
                ->insertAll($arr);
        }else{
            $inserts = 1;
        }


        $insertss = Db::table('fa_oa_relation')
            ->insertGetId([
                'log_id' => $id,
                'admin_id' => $examine[0]['admin_id'],
                'create_time' => time(),
                'is_comment' => 0,
                'status' => 3,
                'type' => 0,
            ]);

        if (($insert !== false) && ($inserts !== false) && ($insertss !== false)){
            //事务提交
            Db::commit();
            $this->success('成功');
        }else{
            //事务回滚
            Db::rollback();
            $this->error('失败');
        }


    }

    /**
     * 请假列表
     *
     */
    public function leave_list(){
        $type = empty(request()->post('type')) ? $this->error('type不能为空'):request()->post('type');

        $page = empty(request()->post('page')) ? 1:request()->post('page');//获取页码
        $limt = empty(request()->post('limt')) ? 10:request()->post('limt');//获取每页显示条数

        //分页
        if ($page == 1) {
            $page_start = 0;
            $page_end = $limt-1;
        } else {
            $page_start = ($page - 1) * $limt;
            $page_end = $page_start + $limt-1;
        }

        if ($type == 1){

            $data = Db::table('fa_oa_leave')
                ->alias('a')
                ->field('a.id,b.nickname,b.avatar,a.create_time,a.status,a.start_time,a.end_time,a.type')
                ->join('fa_admin b','a.admin_id=b.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->limit($page_start,$limt)
                ->order('id','desc')
                ->select();

            $total = Db::table('fa_oa_leave')
                ->alias('a')
                ->join('fa_admin b','a.admin_id=b.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->count();
        }elseif ($type == 2){

            $data = Db::table('fa_oa_relation')
                ->alias('a')
                ->field('b.id,c.nickname,c.avatar,b.create_time,b.status,b.start_time,b.end_time,b.type')
                ->join('fa_oa_leave b','a.log_id=b.id','left')
                ->join('fa_admin c','b.admin_id=c.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->where(['a.status'=>3,'a.type'=>0])
                ->limit($page_start,$limt)
                ->order('id','desc')
                ->select();

            $total = Db::table('fa_oa_relation')
                ->alias('a')
                ->join('fa_oa_leave b','a.log_id=b.id','left')
                ->join('fa_admin c','b.admin_id=c.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->where(['a.status'=>3,'a.type'=>0])
                ->count();
        }elseif ($type == 3){

            $data = Db::table('fa_oa_relation')
                ->alias('a')
                ->field('b.id,c.nickname,c.avatar,b.create_time,b.status,b.start_time,b.end_time,b.type')
                ->join('fa_oa_leave b','a.log_id=b.id','left')
                ->join('fa_admin c','b.admin_id=c.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->where(['a.status'=>3,'a.type'=>1])
                ->limit($page_start,$limt)
                ->order('id','desc')
                ->select();


            $total = Db::table('fa_oa_relation')
                ->alias('a')
                ->join('fa_oa_leave b','a.log_id=b.id','left')
                ->join('fa_admin c','b.admin_id=c.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->where(['a.status'=>3,'a.type'=>1])
                ->count();
        }


        if (!empty($data)){
            foreach ($data as $k=>$v){
                $data[$k]['nickname'] = $v['nickname'].'提交的请假';
                $data[$k]['create_time'] = date('Y-m-d',$v['create_time']);

                if ($v['status'] == 0){
                    $data[$k]['status'] = '待审核';
                }elseif ($v['status'] == 1){
                    $data[$k]['status'] = '审核通过';
                }elseif ($v['status'] == 2){
                    $data[$k]['status'] = '审核驳回';
                }else{
                    $data[$k]['status'] = '';
                }

                //1代表事假，2代表病假，3代表年假，4代表婚假，5代表产假，6代表陪产假，7代表丧假
                switch ($v['type']){
                    case 1:
                        $data[$k]['type'] = '事假';
                        break;
                    case 2:
                        $data[$k]['type'] = '病假';
                        break;
                    case 3:
                        $data[$k]['type'] = '年假';
                        break;
                    case 4:
                        $data[$k]['type'] = '婚假';
                        break;
                    case 5:
                        $data[$k]['type'] = '产假';
                        break;
                    case 6:
                        $data[$k]['type'] = '陪产假';
                        break;
                    case 7:
                        $data[$k]['type'] = '丧假';
                        break;
                    default:
                        $data[$k]['type'] = '';
                        break;
                }
            }
        }


        //组装数据
        $datas = [
            'page' => $page,
            'total' => $total,
            'limt' => $limt,
            'list' => $data,
        ];

        $this->success('成功',$datas);
    }

    /**
     * 请假详情
     *
     */
    public function leave_detail(){
        $id = empty(request()->post('id')) ? $this->error('id不能为空'):request()->post('id');

        $data = Db::table('fa_oa_leave')
            ->field('id,admin_id,code,type,start_time,end_time,day,msg,images,create_time,status as statuss,ids')
            ->where('id',$id)
            ->find();

        if (!empty($data)){
            $data['create_time'] = date('Y-m-d H:i:s',$data['create_time']);

            $name = Db::table('fa_admin')
                ->field('id,nickname')
                ->find();
            if (empty($name)){
                $data['names'] = '';
            }else{
                $data['names'] = $name['nickname'].'提交的请假';
            }


            $news = new Auth;
            $groups = $news->getGroups($data['admin_id']);

            foreach ($groups as $m => $n) {
                $adminGroupName[$data['admin_id']][$n['id']] = $n['name'];
            }

            $groups = isset($adminGroupName[$data['admin_id']]) ? $adminGroupName[$data['admin_id']] : [];

            if (!empty($groups)){
                foreach ($groups as $k=>&$v){
                    $v = __($v);
                }
            }

            $data['groups'] = implode(',', array_values($groups));

            //1代表事假，2代表病假，3代表年假，4代表婚假，5代表产假，6代表陪产假，7代表丧假
            switch ($data['type']){
                case 1:
                    $data['type'] = '事假';
                    break;
                case 2:
                    $data['type'] = '病假';
                    break;
                case 3:
                    $data['type'] = '年假';
                    break;
                case 4:
                    $data['type'] = '婚假';
                    break;
                case 5:
                    $data['type'] = '产假';
                    break;
                case 6:
                    $data['type'] = '陪产假';
                    break;
                case 7:
                    $data['type'] = '丧假';
                    break;
                default:
                    $data['type'] = '';
                    break;
            }

            if (!empty($data['images'])){
                $data['images'] = explode(',',$data['images']);
            }else{
                $data['images'] = [];
            }

            $data['send'] = Db::table('fa_oa_relation')
                ->alias('a')
                ->field('a.id,b.nickname,b.avatar')
                ->join('fa_admin b','a.admin_id=b.id','left')
                ->where(['a.log_id'=>$data['id'],'a.status'=>3,'a.type'=>1])
                ->select();


            $arr = Db::table('fa_admin')
                ->field('id,nickname,avatar')
                ->where('id','in',$data['ids'])
                ->select();


            $data['status'] = '';
            $arrs = [];
            foreach ($arr as $k=>$v){
                $pan = Db::table('fa_oa_relation')
                    ->where(['log_id'=>$data['id'],'admin_id'=>$v['id'],'status'=>3,'type'=>0])
                    ->find();

                if (empty($pan)){
                    $status = '';
                    $msg = '';
                    $create_time = '';
                }else{

                    if ($pan['is_comment'] == 0){
                        $status = '待审核';
                    }elseif ($pan['is_comment'] == 1){
                        $status = '审核通过';
                    }elseif ($pan['is_comment'] == 2){
                        $status = '审核驳回';
                    }else{
                        $status = '无';
                    }

                    if ($v['id'] == $this->user_arr['id']){
                        $data['status'] = $pan['is_comment'];
                    }

                    $msg = $pan['msg'];
                    if (empty($pan['update_time'])){
                        $create_time = '';
                    }else{
                        $create_time = date('Y-m-d H:i:s',$pan['update_time']);
                    }

                }

                $arrs[$k] = [
                    'nickname' => $v['nickname'],
                    'avatar' => $v['avatar'],
                    'create_time' => $create_time,
                    'status' => $status,
                    'msg' => $msg,
                ];
            }

            $adim_arr = Db::table('fa_admin')
                ->where('id',$data['admin_id'])
                ->find();


            $arr_z[0] = [
                'nickname' => $adim_arr['nickname'],
                'avatar' => $adim_arr['avatar'],
                'create_time' => $data['create_time'],
                'status' => '发起申请',
                'msg' => '',
            ];

            $arrss = array_merge($arr_z,$arrs);

            $data['examine'] = $arrss;
            unset($data['ids']);
        }

        $this->success('成功',$data);

    }

    /**
     * 新增-出差
     *
     */
    public function add_evection(){
        $msg = empty(request()->post('msg')) ? '':request()->post('msg');
        $images = empty(request()->post('images')) ? '':request()->post('images');

        $detail = empty(input('post.detail/a')) ? $this->error('detail不能为空'):input('post.detail/a');
        $time = empty(request()->post('time')) ? $this->error('time不能为空'):request()->post('time');
        $ids = empty(request()->post('ids')) ? '':request()->post('ids');

        if (!empty($ids)){
            $ids_arr = explode(',',$ids);

            if (count($ids_arr) != count(array_unique($ids_arr))) {
                $this->error(__('抄送人不能重复'));
            }
        }

        $examine = Db::table('fa_oa_examine')
            ->where(['oa_id'=>3])
            ->order('type','asc')
            ->select();

        if (empty($examine)){
            $this->error(__('审批人为空,请去OA流转设置'));
        }

        $str_arr = [];
        foreach ($examine as $k=>$v){
            $str_arr[$k] = $v['admin_id'];
        }

        $string = implode(',', $str_arr);

        //开启事务
        Db::startTrans();

        $insert = Db::table('fa_oa_evection')
            ->insertGetId([
                'admin_id' => $this->user_arr['id'],
                'code' => get_strings(10,2),
                'msg' => $msg,
                'images' => $images,
                'create_time' => time(),
                'ids' => $string,
                'time' => $time,
            ]);

        $id = $insert;//新增的id


        $details = [];

        foreach ($detail as $k=>$v){
            if (empty($v['site'])){
                //事务回滚
                Db::rollback();
                $this->error(__('出差地点不能为空'));
            }

            if (empty($v['start_time'])){
                //事务回滚
                Db::rollback();
                $this->error(__('开始时间不能为空'));
            }

            if (empty($v['end_time'])){
                //事务回滚
                Db::rollback();
                $this->error(__('结束时间不能为空'));
            }

            if (empty($v['day'])){
                //事务回滚
                Db::rollback();
                $this->error(__('时长不能为空'));
            }

            $details[$k] = [
                'oa_id' => $id,
                'site' => $v['site'],
                'start_time' => $v['start_time'],
                'end_time' => $v['end_time'],
                'day' => $v['day'],
                'create_time' => time(),
            ];

        }
        //新增行程明细
        $insertall = Db::table('fa_oa_route')
            ->insertAll($details);


        $arr = [];
        if (!empty($ids)){
            foreach ($ids_arr as $k=>$v){
                $arr[$k] = [
                    'log_id' => $id,
                    'admin_id' => $v,
                    'create_time' => time(),
                    'is_comment' => 0,
                    'status' => 4,
                    'type' => 1,
                ];
            }

            $inserts = Db::table('fa_oa_relation')
                ->insertAll($arr);
        }else{
            $inserts = 1;
        }


        $insertss = Db::table('fa_oa_relation')
            ->insertGetId([
                'log_id' => $id,
                'admin_id' => $examine[0]['admin_id'],
                'create_time' => time(),
                'is_comment' => 0,
                'status' => 4,
                'type' => 0,
            ]);

        if (($insert !== false) && ($insertall !== false) && ($inserts !== false) && ($insertss !== false)){
            //事务提交
            Db::commit();
            $this->success('成功');
        }else{
            //事务回滚
            Db::rollback();
            $this->error('失败');
        }


    }

    /**
     * 出差列表
     *
     */
    public function evection_list(){
        $type = empty(request()->post('type')) ? $this->error('type不能为空'):request()->post('type');

        $page = empty(request()->post('page')) ? 1:request()->post('page');//获取页码
        $limt = empty(request()->post('limt')) ? 10:request()->post('limt');//获取每页显示条数

        //分页
        if ($page == 1) {
            $page_start = 0;
            $page_end = $limt-1;
        } else {
            $page_start = ($page - 1) * $limt;
            $page_end = $page_start + $limt-1;
        }


        if ($type == 1){
            $data = Db::table('fa_oa_evection')
                ->alias('a')
                ->field('a.id,b.nickname,b.avatar,a.create_time,a.status,a.msg')
                ->join('fa_admin b','a.admin_id=b.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->limit($page_start,$limt)
                ->order('id','desc')
                ->select();

            $total = Db::table('fa_oa_evection')
                ->alias('a')
                ->join('fa_admin b','a.admin_id=b.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->count();
        }elseif ($type == 2){

            $data = Db::table('fa_oa_relation')
                ->alias('a')
                ->field('b.id,c.nickname,c.avatar,b.create_time,b.status,b.msg')
                ->join('fa_oa_evection b','a.log_id=b.id','left')
                ->join('fa_admin c','b.admin_id=c.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->where('a.status',4)
                ->where('a.type',0)
                ->limit($page_start,$limt)
                ->order('id','desc')
                ->select();

            $total = Db::table('fa_oa_relation')
                ->alias('a')
                ->join('fa_oa_evection b','a.log_id=b.id','left')
                ->join('fa_admin c','b.admin_id=c.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->where('a.status',4)
                ->where('a.type',0)
                ->count();
        }elseif ($type == 3){
            $data = Db::table('fa_oa_relation')
                ->alias('a')
                ->field('b.id,c.nickname,c.avatar,b.create_time,b.status,b.msg')
                ->join('fa_oa_evection b','a.log_id=b.id','left')
                ->join('fa_admin c','b.admin_id=c.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->where('a.status',4)
                ->where('a.type',1)
                ->limit($page_start,$limt)
                ->order('id','desc')
                ->select();

            $total = Db::table('fa_oa_relation')
                ->alias('a')
                ->join('fa_oa_evection b','a.log_id=b.id','left')
                ->join('fa_admin c','b.admin_id=c.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->where('a.status',4)
                ->where('a.type',1)
                ->count();
        }


        if (!empty($data)){
            foreach ($data as $k=>$v){
                $data[$k]['nickname'] = $v['nickname'].'提交的出差';
                $data[$k]['create_time'] = date('Y-m-d',$v['create_time']);

                if ($v['status'] == 0){
                    $data[$k]['status'] = '待审核';
                }elseif ($v['status'] == 1){
                    $data[$k]['status'] = '审核通过';
                }elseif ($v['status'] == 2){
                    $data[$k]['status'] = '审核驳回';
                }else{
                    $data[$k]['status'] = '';
                }
            }
        }


        //组装数据
        $datas = [
            'page' => $page,
            'total' => $total,
            'limt' => $limt,
            'list' => $data,
        ];

        $this->success('成功',$datas);
    }

    /**
     * 出差详情
     *
     */
    public function evection_detail(){
        $id = empty(request()->post('id')) ? $this->error('id不能为空'):request()->post('id');

        $data = Db::table('fa_oa_evection')
            ->field('id,admin_id,code,msg,images,time,create_time,ids,status as statuss')
            ->where('id',$id)
            ->find();

        if (!empty($data)){
            $data['create_time'] = date('Y-m-d H:i:s',$data['create_time']);
            $name = Db::table('fa_admin')
                ->field('id,nickname')
                ->find();
            if (empty($name)){
                $data['names'] = '';
            }else{
                $data['names'] = $name['nickname'].'提交的出差';
            }


            if (!empty($data['images'])){
                $data['images'] = explode(',',$data['images']);
            }else{
                $data['images'] = [];
            }

            $data['detail'] = Db::table('fa_oa_route')
                ->field('id,site,start_time,end_time,day')
                ->where('oa_id',$data['id'])
                ->select();

            $data['send'] = Db::table('fa_oa_relation')
                ->alias('a')
                ->field('a.id,b.nickname,b.avatar')
                ->join('fa_admin b','a.admin_id=b.id','left')
                ->where(['a.log_id'=>$data['id'],'a.status'=>4,'a.type'=>1])
                ->select();

            $arr = Db::table('fa_admin')
                ->field('id,nickname,avatar')
                ->where('id','in',$data['ids'])
                ->select();

            $data['status'] = '';
            $arrs = [];
            foreach ($arr as $k=>$v){
                $pan = Db::table('fa_oa_relation')
                    ->where(['log_id'=>$data['id'],'admin_id'=>$v['id'],'status'=>4,'type'=>0])
                    ->find();

                if (empty($pan)){
                    $status = '';
                    $msg = '';
                    $create_time = '';
                }else{

                    if ($pan['is_comment'] == 0){
                        $status = '待审核';
                    }elseif ($pan['is_comment'] == 1){
                        $status = '审核通过';
                    }elseif ($pan['is_comment'] == 2){
                        $status = '审核驳回';
                    }else{
                        $status = '无';
                    }

                    if ($v['id'] == $this->user_arr['id']){
                        $data['status'] = $pan['is_comment'];
                    }


                    $msg = $pan['msg'];
                    if (empty($pan['update_time'])){
                        $create_time = '';
                    }else{
                        $create_time = date('Y-m-d H:i:s',$pan['update_time']);
                    }

                }

                $arrs[$k] = [
                    'nickname' => $v['nickname'],
                    'avatar' => $v['avatar'],
                    'create_time' => $create_time,
                    'status' => $status,
                    'msg' => $msg,
                ];
            }

            $adim_arr = Db::table('fa_admin')
                ->where('id',$data['admin_id'])
                ->find();


            $arr_z[0] = [
                'nickname' => $adim_arr['nickname'],
                'avatar' => $adim_arr['avatar'],
                'create_time' => $data['create_time'],
                'status' => '发起申请',
                'msg' => '',
            ];

            $arrss = array_merge($arr_z,$arrs);

            $data['examine'] = $arrss;
            unset($data['ids']);

        }

        $this->success('成功',$data);

    }


    /**
     * 新增-通用审批
     *
     */
    public function add_universal(){
        $content = empty(request()->post('content')) ? $this->error('content不能为空'):request()->post('content');
        $details = empty(request()->post('details')) ? $this->error('details不能为空'):request()->post('details');

        $images = empty(request()->post('images')) ? '':request()->post('images');
        $ids = empty(request()->post('ids')) ? '':request()->post('ids');

        $pan_count = explode(',',$images);
        if (count($pan_count) > 9){
            $this->error(__('数量最大添加9张'));
        }

        if (!empty($ids)){
            $ids_arr = explode(',',$ids);

            if (count($ids_arr) != count(array_unique($ids_arr))) {
                $this->error(__('抄送人不能重复'));
            }
        }

        $examine = Db::table('fa_oa_examine')
            ->where(['oa_id'=>4])
            ->order('type','asc')
            ->select();

        if (empty($examine)){
            $this->error(__('审批人为空,请去OA流转设置'));
        }

        $str_arr = [];
        foreach ($examine as $k=>$v){
            $str_arr[$k] = $v['admin_id'];
        }

        $string = implode(',', $str_arr);

        //开启事务
        Db::startTrans();

        $insert = Db::table('fa_oa_universal')
            ->insertGetId([
                'admin_id' => $this->user_arr['id'],
                'code' => get_strings(10,2),
                'content' => $content,
                'details' => $details,
                'images' => $images,
                'create_time' => time(),
                'ids' => $string,
            ]);

        $id = $insert;//新增的id

        $arr = [];
        if (!empty($ids)){
            foreach ($ids_arr as $k=>$v){
                $arr[$k] = [
                    'log_id' => $id,
                    'admin_id' => $v,
                    'create_time' => time(),
                    'is_comment' => 0,
                    'status' => 5,
                    'type' => 1,
                ];
            }

            $inserts = Db::table('fa_oa_relation')
                ->insertAll($arr);
        }else{
            $inserts = 1;
        }


        $insertss = Db::table('fa_oa_relation')
            ->insertGetId([
                'log_id' => $id,
                'admin_id' => $examine[0]['admin_id'],
                'create_time' => time(),
                'is_comment' => 0,
                'status' => 5,
                'type' => 0,
            ]);

        if (($insert !== false) && ($inserts !== false) && ($insertss !== false)){
            //事务提交
            Db::commit();
            $this->success('成功');
        }else{
            //事务回滚
            Db::rollback();
            $this->error('失败');
        }


    }

    /**
     * 通用审批列表
     *
     */
    public function universal_list(){
        $type = empty(request()->post('type')) ? $this->error('type不能为空'):request()->post('type');

        $page = empty(request()->post('page')) ? 1:request()->post('page');//获取页码
        $limt = empty(request()->post('limt')) ? 10:request()->post('limt');//获取每页显示条数

        //分页
        if ($page == 1) {
            $page_start = 0;
            $page_end = $limt-1;
        } else {
            $page_start = ($page - 1) * $limt;
            $page_end = $page_start + $limt-1;
        }

        if ($type == 1){

            $data = Db::table('fa_oa_universal')
                ->alias('a')
                ->field('a.id,b.nickname,b.avatar,a.create_time,a.status,a.content')
                ->join('fa_admin b','a.admin_id=b.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->limit($page_start,$limt)
                ->order('id','desc')
                ->select();

            $total = Db::table('fa_oa_universal')
                ->alias('a')
                ->join('fa_admin b','a.admin_id=b.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->count();
        }elseif ($type == 2){

            $data = Db::table('fa_oa_relation')
                ->alias('a')
                ->field('b.id,c.nickname,c.avatar,b.create_time,b.status,b.content')
                ->join('fa_oa_universal b','a.log_id=b.id','left')
                ->join('fa_admin c','b.admin_id=c.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->where(['a.status'=>5,'a.type'=>0])
                ->limit($page_start,$limt)
                ->order('id','desc')
                ->select();

            $total = Db::table('fa_oa_relation')
                ->alias('a')
                ->join('fa_oa_universal b','a.log_id=b.id','left')
                ->join('fa_admin c','b.admin_id=c.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->where(['a.status'=>5,'a.type'=>0])
                ->count();
        }elseif ($type == 3){

            $data = Db::table('fa_oa_relation')
                ->alias('a')
                ->field('b.id,c.nickname,c.avatar,b.create_time,b.status,b.content')
                ->join('fa_oa_universal b','a.log_id=b.id','left')
                ->join('fa_admin c','b.admin_id=c.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->where(['a.status'=>5,'a.type'=>1])
                ->limit($page_start,$limt)
                ->order('id','desc')
                ->select();


            $total = Db::table('fa_oa_relation')
                ->alias('a')
                ->join('fa_oa_universal b','a.log_id=b.id','left')
                ->join('fa_admin c','b.admin_id=c.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->where(['a.status'=>5,'a.type'=>1])
                ->count();
        }


        if (!empty($data)){
            foreach ($data as $k=>$v){
                $data[$k]['nickname'] = $v['nickname'].'提交的通用审批';
                $data[$k]['create_time'] = date('Y-m-d',$v['create_time']);

                if ($v['status'] == 0){
                    $data[$k]['status'] = '待审核';
                }elseif ($v['status'] == 1){
                    $data[$k]['status'] = '审核通过';
                }elseif ($v['status'] == 2){
                    $data[$k]['status'] = '审核驳回';
                }else{
                    $data[$k]['status'] = '';
                }
            }
        }


        //组装数据
        $datas = [
            'page' => $page,
            'total' => $total,
            'limt' => $limt,
            'list' => $data,
        ];

        $this->success('成功',$datas);
    }

    /**
     * 通用审批详情
     *
     */
    public function universal_detail(){
        $id = empty(request()->post('id')) ? $this->error('id不能为空'):request()->post('id');

        $data = Db::table('fa_oa_universal')
            ->field('id,admin_id,code,content,details,images,create_time,status as statuss,ids')
            ->where('id',$id)
            ->find();

        if (!empty($data)){
            $data['create_time'] = date('Y-m-d H:i:s',$data['create_time']);

            $name = Db::table('fa_admin')
                ->field('id,nickname')
                ->find();
            if (empty($name)){
                $data['names'] = '';
            }else{
                $data['names'] = $name['nickname'].'提交的通用审批';
            }

            if (!empty($data['images'])){
                $data['images'] = explode(',',$data['images']);
            }else{
                $data['images'] = [];
            }

            $data['send'] = Db::table('fa_oa_relation')
                ->alias('a')
                ->field('a.id,b.nickname,b.avatar')
                ->join('fa_admin b','a.admin_id=b.id','left')
                ->where(['a.log_id'=>$data['id'],'a.status'=>5,'a.type'=>1])
                ->select();


            $arr = Db::table('fa_admin')
                ->field('id,nickname,avatar')
                ->where('id','in',$data['ids'])
                ->select();


            $data['status'] = '';
            $arrs = [];
            foreach ($arr as $k=>$v){
                $pan = Db::table('fa_oa_relation')
                    ->where(['log_id'=>$data['id'],'admin_id'=>$v['id'],'status'=>5,'type'=>0])
                    ->find();

                if (empty($pan)){
                    $status = '';
                    $msg = '';
                    $create_time = '';
                }else{

                    if ($pan['is_comment'] == 0){
                        $status = '待审核';
                    }elseif ($pan['is_comment'] == 1){
                        $status = '审核通过';
                    }elseif ($pan['is_comment'] == 2){
                        $status = '审核驳回';
                    }else{
                        $status = '无';
                    }

                    if ($v['id'] == $this->user_arr['id']){
                        $data['status'] = $pan['is_comment'];
                    }

                    $msg = $pan['msg'];
                    if (empty($pan['update_time'])){
                        $create_time = '';
                    }else{
                        $create_time = date('Y-m-d H:i:s',$pan['update_time']);
                    }

                }

                $arrs[$k] = [
                    'nickname' => $v['nickname'],
                    'avatar' => $v['avatar'],
                    'create_time' => $create_time,
                    'status' => $status,
                    'msg' => $msg,
                ];
            }

            $adim_arr = Db::table('fa_admin')
                ->where('id',$data['admin_id'])
                ->find();


            $arr_z[0] = [
                'nickname' => $adim_arr['nickname'],
                'avatar' => $adim_arr['avatar'],
                'create_time' => $data['create_time'],
                'status' => '发起申请',
                'msg' => '',
            ];

            $arrss = array_merge($arr_z,$arrs);

            $data['examine'] = $arrss;
            unset($data['ids']);
        }

        $this->success('成功',$data);

    }

    /**
     * 新增-报销
     *
     */
    public function add_apply(){
        $detail = empty(input('post.detail/a')) ? $this->error('detail不能为空'):input('post.detail/a');

        $amount = empty(request()->post('amount')) ? $this->error('amount不能为空'):request()->post('amount');
        $images = empty(request()->post('images')) ? $this->error('images不能为空'):request()->post('images');

        $ids = empty(request()->post('ids')) ? '':request()->post('ids');

        $pan_count = explode(',',$images);
        if (count($pan_count) > 9){
            $this->error(__('数量最大添加9张'));
        }

        if (!empty($ids)){
            $ids_arr = explode(',',$ids);

            if (count($ids_arr) != count(array_unique($ids_arr))) {
                $this->error(__('抄送人不能重复'));
            }
        }

        $examine = Db::table('fa_oa_examine')
            ->where(['oa_id'=>5])
            ->order('type','asc')
            ->select();

        if (empty($examine)){
            $this->error(__('审批人为空,请去OA流转设置'));
        }

        $str_arr = [];
        foreach ($examine as $k=>$v){
            $str_arr[$k] = $v['admin_id'];
        }

        $string = implode(',', $str_arr);

        //开启事务
        Db::startTrans();

        $insert = Db::table('fa_oa_apply')
            ->insertGetId([
                'admin_id' => $this->user_arr['id'],
                'code' => get_strings(10,2),
                'amount' => $amount,
                'images' => $images,
                'create_time' => time(),
                'ids' => $string,
            ]);

        $id = $insert;//新增的id


        $details = [];

        foreach ($detail as $k=>$v){
            if (empty($v['money'])){
                //事务回滚
                Db::rollback();
                $this->error(__('报销金额不能为空'));
            }

            if (empty($v['type'])){
                //事务回滚
                Db::rollback();
                $this->error(__('报销类别不能为空'));
            }

            if (empty($v['cost'])){
                //事务回滚
                Db::rollback();
                $this->error(__('费用明细不能为空'));
            }

            $details[$k] = [
                'oa_id' => $id,
                'money' => $v['money'],
                'type' => $v['type'],
                'cost' => $v['cost'],
                'create_time' => time(),
            ];

        }
        //新增报销明细
        $insertall = Db::table('fa_oa_cost')
            ->insertAll($details);


        $arr = [];
        if (!empty($ids)){
            foreach ($ids_arr as $k=>$v){
                $arr[$k] = [
                    'log_id' => $id,
                    'admin_id' => $v,
                    'create_time' => time(),
                    'is_comment' => 0,
                    'status' => 6,
                    'type' => 1,
                ];
            }

            $inserts = Db::table('fa_oa_relation')
                ->insertAll($arr);
        }else{
            $inserts = 1;
        }


        $insertss = Db::table('fa_oa_relation')
            ->insertGetId([
                'log_id' => $id,
                'admin_id' => $examine[0]['admin_id'],
                'create_time' => time(),
                'is_comment' => 0,
                'status' => 6,
                'type' => 0,
            ]);

        if (($insert !== false) && ($insertall !== false) && ($inserts !== false) && ($insertss !== false)){
            //事务提交
            Db::commit();
            $this->success('成功');
        }else{
            //事务回滚
            Db::rollback();
            $this->error('失败');
        }


    }


    /**
     * 报销列表
     *
     */
    public function apply_list(){
        $type = empty(request()->post('type')) ? $this->error('type不能为空'):request()->post('type');

        $page = empty(request()->post('page')) ? 1:request()->post('page');//获取页码
        $limt = empty(request()->post('limt')) ? 10:request()->post('limt');//获取每页显示条数

        //分页
        if ($page == 1) {
            $page_start = 0;
            $page_end = $limt-1;
        } else {
            $page_start = ($page - 1) * $limt;
            $page_end = $page_start + $limt-1;
        }


        if ($type == 1){
            $data = Db::table('fa_oa_apply')
                ->alias('a')
                ->field('a.id,b.nickname,b.avatar,a.create_time,a.status,a.code,a.images')
                ->join('fa_admin b','a.admin_id=b.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->limit($page_start,$limt)
                ->order('id','desc')
                ->select();

            $total = Db::table('fa_oa_apply')
                ->alias('a')
                ->join('fa_admin b','a.admin_id=b.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->count();
        }elseif ($type == 2){

            $data = Db::table('fa_oa_relation')
                ->alias('a')
                ->field('b.id,c.nickname,c.avatar,b.create_time,b.status,b.code,b.images')
                ->join('fa_oa_apply b','a.log_id=b.id','left')
                ->join('fa_admin c','b.admin_id=c.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->where('a.status',6)
                ->where('a.type',0)
                ->limit($page_start,$limt)
                ->order('id','desc')
                ->select();

            $total = Db::table('fa_oa_relation')
                ->alias('a')
                ->join('fa_oa_apply b','a.log_id=b.id','left')
                ->join('fa_admin c','b.admin_id=c.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->where('a.status',6)
                ->where('a.type',0)
                ->count();
        }elseif ($type == 3){
            $data = Db::table('fa_oa_relation')
                ->alias('a')
                ->field('b.id,c.nickname,c.avatar,b.create_time,b.status,b.code,b.images')
                ->join('fa_oa_apply b','a.log_id=b.id','left')
                ->join('fa_admin c','b.admin_id=c.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->where('a.status',6)
                ->where('a.type',1)
                ->limit($page_start,$limt)
                ->order('id','desc')
                ->select();

            $total = Db::table('fa_oa_relation')
                ->alias('a')
                ->join('fa_oa_apply b','a.log_id=b.id','left')
                ->join('fa_admin c','b.admin_id=c.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->where('a.status',6)
                ->where('a.type',1)
                ->count();
        }


        if (!empty($data)){
            foreach ($data as $k=>$v){
                if (!empty($v['images'])){
                    $data[$k]['images'] = explode(',',$v['images']);
                }else{
                    $data[$k]['images'] = [];
                }

                $data[$k]['create_time'] = date('Y-m-d',$v['create_time']);

                if ($v['status'] == 0){
                    $data[$k]['status'] = '待审核';
                }elseif ($v['status'] == 1){
                    $data[$k]['status'] = '审核通过';
                }elseif ($v['status'] == 2){
                    $data[$k]['status'] = '审核驳回';
                }else{
                    $data[$k]['status'] = '';
                }
            }
        }


        //组装数据
        $datas = [
            'page' => $page,
            'total' => $total,
            'limt' => $limt,
            'list' => $data,
        ];

        $this->success('成功',$datas);
    }

    /**
     * 报销详情
     *
     */
    public function apply_detail(){
        $id = empty(request()->post('id')) ? $this->error('id不能为空'):request()->post('id');

        $data = Db::table('fa_oa_apply')
            ->field('id,admin_id,code,amount,images,create_time,ids,status as statuss')
            ->where('id',$id)
            ->find();

        if (!empty($data)){
            $data['create_time'] = date('Y-m-d H:i:s',$data['create_time']);

            if (!empty($data['images'])){
                $data['images'] = explode(',',$data['images']);
            }else{
                $data['images'] = [];
            }

            $data['detail'] = Db::table('fa_oa_cost')
                ->field('id,money,type,cost')
                ->where('oa_id',$data['id'])
                ->select();

            $data['send'] = Db::table('fa_oa_relation')
                ->alias('a')
                ->field('a.id,b.nickname,b.avatar')
                ->join('fa_admin b','a.admin_id=b.id','left')
                ->where(['a.log_id'=>$data['id'],'a.status'=>6,'a.type'=>1])
                ->select();

            $arr = Db::table('fa_admin')
                ->field('id,nickname,avatar')
                ->where('id','in',$data['ids'])
                ->select();

            $data['status'] = '';
            $arrs = [];
            foreach ($arr as $k=>$v){
                $pan = Db::table('fa_oa_relation')
                    ->where(['log_id'=>$data['id'],'admin_id'=>$v['id'],'status'=>6,'type'=>0])
                    ->find();

                if (empty($pan)){
                    $status = '';
                    $msg = '';
                    $create_time = '';
                }else{

                    if ($pan['is_comment'] == 0){
                        $status = '待审核';
                    }elseif ($pan['is_comment'] == 1){
                        $status = '审核通过';
                    }elseif ($pan['is_comment'] == 2){
                        $status = '审核驳回';
                    }else{
                        $status = '无';
                    }

                    if ($v['id'] == $this->user_arr['id']){
                        $data['status'] = $pan['is_comment'];
                    }


                    $msg = $pan['msg'];
                    if (empty($pan['update_time'])){
                        $create_time = '';
                    }else{
                        $create_time = date('Y-m-d H:i:s',$pan['update_time']);
                    }

                }

                $arrs[$k] = [
                    'nickname' => $v['nickname'],
                    'avatar' => $v['avatar'],
                    'create_time' => $create_time,
                    'status' => $status,
                    'msg' => $msg,
                ];
            }

            $adim_arr = Db::table('fa_admin')
                ->where('id',$data['admin_id'])
                ->find();


            $arr_z[0] = [
                'nickname' => $adim_arr['nickname'],
                'avatar' => $adim_arr['avatar'],
                'create_time' => $data['create_time'],
                'status' => '发起申请',
                'msg' => '',
            ];

            $arrss = array_merge($arr_z,$arrs);

            $data['examine'] = $arrss;
            unset($data['ids']);

        }

        $this->success('成功',$data);

    }

    /**
     * 新增-用章申请
     *
     */
    public function add_stamp(){
        $incident = empty(request()->post('incident')) ? $this->error('incident不能为空'):request()->post('incident');
        $date = empty(request()->post('date')) ? $this->error('date不能为空'):request()->post('date');
        $admin_name = empty(request()->post('admin_name')) ? $this->error('admin_name不能为空'):request()->post('admin_name');
        $group = empty(request()->post('group')) ? $this->error('group不能为空'):request()->post('group');
        $file_name = empty(request()->post('file_name')) ? $this->error('file_name不能为空'):request()->post('file_name');
        $count = empty(request()->post('count')) ? $this->error('count不能为空'):request()->post('count');
        $type = empty(request()->post('type')) ? $this->error('type不能为空'):request()->post('type');

        $file = empty(request()->post('file')) ? '':request()->post('file');
        $ids = empty(request()->post('ids')) ? '':request()->post('ids');

        $pan_count = explode(',',$file);
        if (count($pan_count) > 3){
            $this->error(__('数量最大添加3份'));
        }


        if (!empty($ids)){
            $ids_arr = explode(',',$ids);

            if (count($ids_arr) != count(array_unique($ids_arr))) {
                $this->error(__('抄送人不能重复'));
            }
        }

        $examine = Db::table('fa_oa_examine')
            ->where(['oa_id'=>11])
            ->order('type','asc')
            ->select();

        if (empty($examine)){
            $this->error(__('审批人为空,请去OA流转设置'));
        }

        $str_arr = [];
        foreach ($examine as $k=>$v){
            $str_arr[$k] = $v['admin_id'];
        }

        $string = implode(',', $str_arr);

        //开启事务
        Db::startTrans();

        $insert = Db::table('fa_oa_stamp')
            ->insertGetId([
                'admin_id' => $this->user_arr['id'],
                'code' => get_strings(10,2),
                'incident' => $incident,
                'date' => $date,
                'admin_name' => $admin_name,
                'group' => $group,
                'file_name' => $file_name,
                'count' => $count,
                'type' => $type,
                'file' => $file,
                'create_time' => time(),
                'ids' => $string,
            ]);

        $id = $insert;//新增的id

        $arr = [];
        if (!empty($ids)){
            foreach ($ids_arr as $k=>$v){
                $arr[$k] = [
                    'log_id' => $id,
                    'admin_id' => $v,
                    'create_time' => time(),
                    'is_comment' => 0,
                    'status' => 12,
                    'type' => 1,
                ];
            }

            $inserts = Db::table('fa_oa_relation')
                ->insertAll($arr);
        }else{
            $inserts = 1;
        }


        $insertss = Db::table('fa_oa_relation')
            ->insertGetId([
                'log_id' => $id,
                'admin_id' => $examine[0]['admin_id'],
                'create_time' => time(),
                'is_comment' => 0,
                'status' => 12,
                'type' => 0,
            ]);

        if (($insert !== false) && ($inserts !== false) && ($insertss !== false)){
            //事务提交
            Db::commit();
            $this->success('成功');
        }else{
            //事务回滚
            Db::rollback();
            $this->error('失败');
        }


    }

    /**
     * 用章申请列表
     *
     */
    public function stamp_list(){
        $type = empty(request()->post('type')) ? $this->error('type不能为空'):request()->post('type');

        $page = empty(request()->post('page')) ? 1:request()->post('page');//获取页码
        $limt = empty(request()->post('limt')) ? 10:request()->post('limt');//获取每页显示条数

        //分页
        if ($page == 1) {
            $page_start = 0;
            $page_end = $limt-1;
        } else {
            $page_start = ($page - 1) * $limt;
            $page_end = $page_start + $limt-1;
        }

        if ($type == 1){

            $data = Db::table('fa_oa_stamp')
                ->alias('a')
                ->field('a.id,b.nickname,b.avatar,a.create_time,a.status,a.file_name')
                ->join('fa_admin b','a.admin_id=b.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->limit($page_start,$limt)
                ->order('id','desc')
                ->select();

            $total = Db::table('fa_oa_stamp')
                ->alias('a')
                ->join('fa_admin b','a.admin_id=b.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->count();
        }elseif ($type == 2){

            $data = Db::table('fa_oa_relation')
                ->alias('a')
                ->field('b.id,c.nickname,c.avatar,b.create_time,b.status,b.file_name')
                ->join('fa_oa_stamp b','a.log_id=b.id','left')
                ->join('fa_admin c','b.admin_id=c.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->where(['a.status'=>12,'a.type'=>0])
                ->limit($page_start,$limt)
                ->order('id','desc')
                ->select();

            $total = Db::table('fa_oa_relation')
                ->alias('a')
                ->join('fa_oa_stamp b','a.log_id=b.id','left')
                ->join('fa_admin c','b.admin_id=c.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->where(['a.status'=>12,'a.type'=>0])
                ->count();
        }elseif ($type == 3){

            $data = Db::table('fa_oa_relation')
                ->alias('a')
                ->field('b.id,c.nickname,c.avatar,b.create_time,b.status,b.file_name')
                ->join('fa_oa_stamp b','a.log_id=b.id','left')
                ->join('fa_admin c','b.admin_id=c.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->where(['a.status'=>12,'a.type'=>1])
                ->limit($page_start,$limt)
                ->order('id','desc')
                ->select();


            $total = Db::table('fa_oa_relation')
                ->alias('a')
                ->join('fa_oa_stamp b','a.log_id=b.id','left')
                ->join('fa_admin c','b.admin_id=c.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->where(['a.status'=>12,'a.type'=>1])
                ->count();
        }


        if (!empty($data)){
            foreach ($data as $k=>$v){
                $data[$k]['nickname'] = $v['nickname'].'提交的用章申请';
                $data[$k]['create_time'] = date('Y-m-d',$v['create_time']);

                if ($v['status'] == 0){
                    $data[$k]['status'] = '待审核';
                }elseif ($v['status'] == 1){
                    $data[$k]['status'] = '审核通过';
                }elseif ($v['status'] == 2){
                    $data[$k]['status'] = '审核驳回';
                }else{
                    $data[$k]['status'] = '';
                }
            }
        }


        //组装数据
        $datas = [
            'page' => $page,
            'total' => $total,
            'limt' => $limt,
            'list' => $data,
        ];

        $this->success('成功',$datas);
    }

    /**
     * 用章申请详情
     *
     */
    public function stamp_detail(){
        $id = empty(request()->post('id')) ? $this->error('id不能为空'):request()->post('id');

        $data = Db::table('fa_oa_stamp')
            ->alias('a')
            ->field('a.id,a.admin_id,a.code,a.incident,a.date,a.admin_name,b.name as group_name,a.file_name,a.count,a.type,a.file,a.create_time,a.status as statuss,a.ids')
            ->join('fa_auth_group b','a.group=b.id','left')
            ->where('a.id',$id)
            ->find();

        if (!empty($data)){
            $data['create_time'] = date('Y-m-d H:i:s',$data['create_time']);

            $name = Db::table('fa_admin')
                ->field('id,nickname')
                ->find();
            if (empty($name)){
                $data['names'] = '';
            }else{
                $data['names'] = $name['nickname'].'提交的用章申请';
            }

            if (!empty($data['file'])){
                $data['file'] = explode(',',$data['file']);
            }else{
                $data['file'] = [];
            }

            $data['send'] = Db::table('fa_oa_relation')
                ->alias('a')
                ->field('a.id,b.nickname,b.avatar')
                ->join('fa_admin b','a.admin_id=b.id','left')
                ->where(['a.log_id'=>$data['id'],'a.status'=>12,'a.type'=>1])
                ->select();


            $arr = Db::table('fa_admin')
                ->field('id,nickname,avatar')
                ->where('id','in',$data['ids'])
                ->select();


            $data['status'] = '';
            $arrs = [];
            foreach ($arr as $k=>$v){
                $pan = Db::table('fa_oa_relation')
                    ->where(['log_id'=>$data['id'],'admin_id'=>$v['id'],'status'=>12,'type'=>0])
                    ->find();

                if (empty($pan)){
                    $status = '';
                    $msg = '';
                    $create_time = '';
                }else{

                    if ($pan['is_comment'] == 0){
                        $status = '待审核';
                    }elseif ($pan['is_comment'] == 1){
                        $status = '审核通过';
                    }elseif ($pan['is_comment'] == 2){
                        $status = '审核驳回';
                    }else{
                        $status = '无';
                    }

                    if ($v['id'] == $this->user_arr['id']){
                        $data['status'] = $pan['is_comment'];
                    }

                    $msg = $pan['msg'];
                    if (empty($pan['update_time'])){
                        $create_time = '';
                    }else{
                        $create_time = date('Y-m-d H:i:s',$pan['update_time']);
                    }

                }

                $arrs[$k] = [
                    'nickname' => $v['nickname'],
                    'avatar' => $v['avatar'],
                    'create_time' => $create_time,
                    'status' => $status,
                    'msg' => $msg,
                ];
            }

            $adim_arr = Db::table('fa_admin')
                ->where('id',$data['admin_id'])
                ->find();


            $arr_z[0] = [
                'nickname' => $adim_arr['nickname'],
                'avatar' => $adim_arr['avatar'],
                'create_time' => $data['create_time'],
                'status' => '发起申请',
                'msg' => '',
            ];

            $arrss = array_merge($arr_z,$arrs);

            $data['examine'] = $arrss;
            unset($data['ids']);
        }

        $this->success('成功',$data);

    }


    /**
     * 新增-内购申请
     *
     */
    public function add_buy(){
        $admin_name = empty(request()->post('admin_name')) ? $this->error('admin_name不能为空'):request()->post('admin_name');
        $time = empty(request()->post('time')) ? $this->error('time不能为空'):request()->post('time');
        $group = empty(request()->post('group')) ? $this->error('group不能为空'):request()->post('group');
        $buy = empty(request()->post('buy')) ? $this->error('buy不能为空'):request()->post('buy');

        $detail = empty(input('post.detail/a')) ? $this->error('detail不能为空'):input('post.detail/a');
        $ids = empty(request()->post('ids')) ? '':request()->post('ids');

        if (!empty($ids)){
            $ids_arr = explode(',',$ids);

            if (count($ids_arr) != count(array_unique($ids_arr))) {
                $this->error(__('抄送人不能重复'));
            }
        }

        $examine = Db::table('fa_oa_examine')
            ->where(['oa_id'=>8])
            ->order('type','asc')
            ->select();

        if (empty($examine)){
            $this->error(__('审批人为空,请去OA流转设置'));
        }

        $str_arr = [];
        foreach ($examine as $k=>$v){
            $str_arr[$k] = $v['admin_id'];
        }

        $string = implode(',', $str_arr);

        //开启事务
        Db::startTrans();

        $insert = Db::table('fa_oa_buy')
            ->insertGetId([
                'admin_id' => $this->user_arr['id'],
                'code' => get_strings(10,2),
                'admin_name' => $admin_name,
                'time' => $time,
                'group' => $group,
                'buy' => $buy,
                'create_time' => time(),
                'ids' => $string,
            ]);

        $id = $insert;//新增的id


        $details = [];

        foreach ($detail as $k=>$v){
            if (empty($v['id'])){
                //事务回滚
                Db::rollback();
                $this->error(__('服务id不能为空'));
            }

            if (empty($v['serve'])){
                //事务回滚
                Db::rollback();
                $this->error(__('产品服务不能为空'));
            }

            if (empty($v['count'])){
                //事务回滚
                Db::rollback();
                $this->error(__('数量不能为空'));
            }

            if (empty($v['money'])){
                //事务回滚
                Db::rollback();
                $this->error(__('单价不能为空'));
            }

            if (empty($v['total'])){
                //事务回滚
                Db::rollback();
                $this->error(__('总价不能为空'));
            }

            $details[$k] = [
                'oa_id' => $id,
                'serve' => $v['serve'],
                'count' => $v['count'],
                'money' => $v['money'],
                'total' => $v['total'],
                'create_time' => time(),
                'vip_id' => $v['id'],
            ];

        }
        //新增产品服务明细
        $insertall = Db::table('fa_oa_service')
            ->insertAll($details);


        $arr = [];
        if (!empty($ids)){
            foreach ($ids_arr as $k=>$v){
                $arr[$k] = [
                    'log_id' => $id,
                    'admin_id' => $v,
                    'create_time' => time(),
                    'is_comment' => 0,
                    'status' => 9,
                    'type' => 1,
                ];
            }

            $inserts = Db::table('fa_oa_relation')
                ->insertAll($arr);
        }else{
            $inserts = 1;
        }


        $insertss = Db::table('fa_oa_relation')
            ->insertGetId([
                'log_id' => $id,
                'admin_id' => $examine[0]['admin_id'],
                'create_time' => time(),
                'is_comment' => 0,
                'status' => 9,
                'type' => 0,
            ]);

        if (($insert !== false) && ($insertall !== false) && ($inserts !== false) && ($insertss !== false)){
            //事务提交
            Db::commit();
            $this->success('成功');
        }else{
            //事务回滚
            Db::rollback();
            $this->error('失败');
        }


    }

    /**
     * 内购申请列表
     *
     */
    public function buy_list(){
        $type = empty(request()->post('type')) ? $this->error('type不能为空'):request()->post('type');

        $page = empty(request()->post('page')) ? 1:request()->post('page');//获取页码
        $limt = empty(request()->post('limt')) ? 10:request()->post('limt');//获取每页显示条数

        //分页
        if ($page == 1) {
            $page_start = 0;
            $page_end = $limt-1;
        } else {
            $page_start = ($page - 1) * $limt;
            $page_end = $page_start + $limt-1;
        }


        if ($type == 1){
            $data = Db::table('fa_oa_buy')
                ->alias('a')
                ->field('a.id,b.nickname,b.avatar,a.create_time,a.status,a.admin_name')
                ->join('fa_admin b','a.admin_id=b.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->limit($page_start,$limt)
                ->order('id','desc')
                ->select();

            $total = Db::table('fa_oa_buy')
                ->alias('a')
                ->join('fa_admin b','a.admin_id=b.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->count();
        }elseif ($type == 2){

            $data = Db::table('fa_oa_relation')
                ->alias('a')
                ->field('b.id,c.nickname,c.avatar,b.create_time,b.status,b.admin_name')
                ->join('fa_oa_buy b','a.log_id=b.id','left')
                ->join('fa_admin c','b.admin_id=c.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->where('a.status',9)
                ->where('a.type',0)
                ->limit($page_start,$limt)
                ->order('id','desc')
                ->select();

            $total = Db::table('fa_oa_relation')
                ->alias('a')
                ->join('fa_oa_buy b','a.log_id=b.id','left')
                ->join('fa_admin c','b.admin_id=c.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->where('a.status',9)
                ->where('a.type',0)
                ->count();
        }elseif ($type == 3){
            $data = Db::table('fa_oa_relation')
                ->alias('a')
                ->field('b.id,c.nickname,c.avatar,b.create_time,b.status,b.admin_name')
                ->join('fa_oa_buy b','a.log_id=b.id','left')
                ->join('fa_admin c','b.admin_id=c.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->where('a.status',9)
                ->where('a.type',1)
                ->limit($page_start,$limt)
                ->order('id','desc')
                ->select();

            $total = Db::table('fa_oa_relation')
                ->alias('a')
                ->join('fa_oa_buy b','a.log_id=b.id','left')
                ->join('fa_admin c','b.admin_id=c.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->where('a.status',9)
                ->where('a.type',1)
                ->count();
        }


        if (!empty($data)){
            foreach ($data as $k=>$v){
                $data[$k]['nickname'] = $v['nickname'].'提交的内购申请';
                $data[$k]['create_time'] = date('Y-m-d',$v['create_time']);

                if ($v['status'] == 0){
                    $data[$k]['status'] = '待审核';
                }elseif ($v['status'] == 1){
                    $data[$k]['status'] = '审核通过';
                }elseif ($v['status'] == 2){
                    $data[$k]['status'] = '审核驳回';
                }else{
                    $data[$k]['status'] = '';
                }
            }
        }


        //组装数据
        $datas = [
            'page' => $page,
            'total' => $total,
            'limt' => $limt,
            'list' => $data,
        ];

        $this->success('成功',$datas);
    }


    /**
     * 内购申请详情
     *
     */
    public function buy_detail(){
        $id = empty(request()->post('id')) ? $this->error('id不能为空'):request()->post('id');

        $data = Db::table('fa_oa_buy')
            ->alias('a')
            ->field('a.id,a.admin_id,a.code,a.admin_name,a.time,a.group,a.buy,a.create_time,a.ids,a.status as statuss,b.name as group_name')
            ->join('fa_auth_group b','a.group=b.id','left')
            ->where('a.id',$id)
            ->find();

        if (!empty($data)){
            $data['create_time'] = date('Y-m-d H:i:s',$data['create_time']);

            $name = Db::table('fa_admin')
                ->field('id,nickname')
                ->where('id',$data['admin_id'])
                ->find();
            if (empty($name)){
                $data['names'] = '';
            }else{
                $data['names'] = $name['nickname'].'提交的内购申请';
            }

            $data['detail'] = Db::table('fa_oa_service')
                ->alias('a')
                ->field('a.id,a.count,a.money,a.total,a.vip_id,b.name as serve')
                ->join('fa_vip_serve b','a.vip_id=b.id','left')
                ->where('a.oa_id',$data['id'])
                ->select();

            $data['send'] = Db::table('fa_oa_relation')
                ->alias('a')
                ->field('a.id,b.nickname,b.avatar')
                ->join('fa_admin b','a.admin_id=b.id','left')
                ->where(['a.log_id'=>$data['id'],'a.status'=>9,'a.type'=>1])
                ->select();

            $arr = Db::table('fa_admin')
                ->field('id,nickname,avatar')
                ->where('id','in',$data['ids'])
                ->select();

            $data['status'] = '';
            $arrs = [];
            foreach ($arr as $k=>$v){
                $pan = Db::table('fa_oa_relation')
                    ->where(['log_id'=>$data['id'],'admin_id'=>$v['id'],'status'=>9,'type'=>0])
                    ->find();

                if (empty($pan)){
                    $status = '';
                    $msg = '';
                    $create_time = '';
                }else{

                    if ($pan['is_comment'] == 0){
                        $status = '待审核';
                    }elseif ($pan['is_comment'] == 1){
                        $status = '审核通过';
                    }elseif ($pan['is_comment'] == 2){
                        $status = '审核驳回';
                    }else{
                        $status = '无';
                    }

                    if ($v['id'] == $this->user_arr['id']){
                        $data['status'] = $pan['is_comment'];
                    }


                    $msg = $pan['msg'];
                    if (empty($pan['update_time'])){
                        $create_time = '';
                    }else{
                        $create_time = date('Y-m-d H:i:s',$pan['update_time']);
                    }

                }

                $arrs[$k] = [
                    'nickname' => $v['nickname'],
                    'avatar' => $v['avatar'],
                    'create_time' => $create_time,
                    'status' => $status,
                    'msg' => $msg,
                ];
            }

            $adim_arr = Db::table('fa_admin')
                ->where('id',$data['admin_id'])
                ->find();


            $arr_z[0] = [
                'nickname' => $adim_arr['nickname'],
                'avatar' => $adim_arr['avatar'],
                'create_time' => $data['create_time'],
                'status' => '发起申请',
                'msg' => '',
            ];

            $arrss = array_merge($arr_z,$arrs);

            $data['examine'] = $arrss;
            unset($data['ids']);

        }

        $this->success('成功',$data);

    }

    /**
     * 新增-赠送服务申请
     *
     */
    public function add_give(){
        $info_id = empty(request()->post('info_id')) ? $this->error('info_id不能为空'):request()->post('info_id');
        $type = empty(request()->post('type')) ? $this->error('type不能为空'):request()->post('type');

        $ids = empty(request()->post('ids')) ? '':request()->post('ids');


        if ($type == 1){
            $detail = empty(input('post.detail/a')) ? $this->error('detail不能为空'):input('post.detail/a');
            $apply_money = 0;
        }elseif ($type == 2){
            $detail = [];
            $apply_money = empty(request()->post('apply_money')) ? $this->error('apply_money不能为空'):request()->post('apply_money');
        }else{
            $this->error(__('type参数错误'));
        }

        if (!empty($ids)){
            $ids_arr = explode(',',$ids);

            if (count($ids_arr) != count(array_unique($ids_arr))) {
                $this->error(__('抄送人不能重复'));
            }
        }

        $examine = Db::table('fa_oa_examine')
            ->where(['oa_id'=>9])
            ->order('type','asc')
            ->select();

        if (empty($examine)){
            $this->error(__('审批人为空,请去OA流转设置'));
        }

        $str_arr = [];
        foreach ($examine as $k=>$v){
            $str_arr[$k] = $v['admin_id'];
        }

        $string = implode(',', $str_arr);

        //开启事务
        Db::startTrans();

        $insert = Db::table('fa_oa_give')
            ->insertGetId([
                'admin_id' => $this->user_arr['id'],
                'code' => get_strings(10,2),
                'info_id' => $info_id,
                'type' => $type,
                'apply_money' => $apply_money,
                'create_time' => time(),
                'ids' => $string,
            ]);

        $id = $insert;//新增的id


        if (!empty($detail)){
            $details = [];

            foreach ($detail as $k=>$v){
                if (empty($v['id'])){
                    //事务回滚
                    Db::rollback();
                    $this->error(__('服务id不能为空'));
                }

                if (empty($v['serve'])){
                    //事务回滚
                    Db::rollback();
                    $this->error(__('产品服务不能为空'));
                }

                if (empty($v['count'])){
                    //事务回滚
                    Db::rollback();
                    $this->error(__('数量不能为空'));
                }

                if (empty($v['money'])){
                    //事务回滚
                    Db::rollback();
                    $this->error(__('单价不能为空'));
                }

                if (empty($v['total'])){
                    //事务回滚
                    Db::rollback();
                    $this->error(__('总价不能为空'));
                }

                $details[$k] = [
                    'oa_id' => $id,
                    'serve' => $v['serve'],
                    'count' => $v['count'],
                    'money' => $v['money'],
                    'total' => $v['total'],
                    'create_time' => time(),
                    'vip_id' => $v['id'],
                ];

            }
            //新增产品服务明细
            $insertall = Db::table('fa_oa_services')
                ->insertAll($details);
        }else{
            $insertall = 1;
        }




        $arr = [];
        if (!empty($ids)){
            foreach ($ids_arr as $k=>$v){
                $arr[$k] = [
                    'log_id' => $id,
                    'admin_id' => $v,
                    'create_time' => time(),
                    'is_comment' => 0,
                    'status' => 10,
                    'type' => 1,
                ];
            }

            $inserts = Db::table('fa_oa_relation')
                ->insertAll($arr);
        }else{
            $inserts = 1;
        }


        $insertss = Db::table('fa_oa_relation')
            ->insertGetId([
                'log_id' => $id,
                'admin_id' => $examine[0]['admin_id'],
                'create_time' => time(),
                'is_comment' => 0,
                'status' => 10,
                'type' => 0,
            ]);

        if (($insert !== false) && ($insertall !== false) && ($inserts !== false) && ($insertss !== false)){
            //事务提交
            Db::commit();
            $this->success('成功');
        }else{
            //事务回滚
            Db::rollback();
            $this->error('失败');
        }


    }

    /**
     * 赠送服务申请列表
     *
     */
    public function give_list(){
        $type = empty(request()->post('type')) ? $this->error('type不能为空'):request()->post('type');

        $page = empty(request()->post('page')) ? 1:request()->post('page');//获取页码
        $limt = empty(request()->post('limt')) ? 10:request()->post('limt');//获取每页显示条数

        //分页
        if ($page == 1) {
            $page_start = 0;
            $page_end = $limt-1;
        } else {
            $page_start = ($page - 1) * $limt;
            $page_end = $page_start + $limt-1;
        }


        if ($type == 1){
            $data = Db::table('fa_oa_give')
                ->alias('a')
                ->field('a.id,b.nickname,c.name,a.create_time,a.status')
                ->join('fa_admin b','a.admin_id=b.id','left')
                ->join('fa_info c','a.info_id=c.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->limit($page_start,$limt)
                ->order('id','desc')
                ->select();

            $total = Db::table('fa_oa_give')
                ->alias('a')
                ->join('fa_admin b','a.admin_id=b.id','left')
                ->join('fa_info c','a.info_id=c.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->count();
        }elseif ($type == 2){

            $data = Db::table('fa_oa_relation')
                ->alias('a')
                ->field('b.id,c.nickname,d.name,b.create_time,b.status')
                ->join('fa_oa_give b','a.log_id=b.id','left')
                ->join('fa_admin c','b.admin_id=c.id','left')
                ->join('fa_info d','b.info_id=d.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->where('a.status',10)
                ->where('a.type',0)
                ->limit($page_start,$limt)
                ->order('id','desc')
                ->select();

            $total = Db::table('fa_oa_relation')
                ->alias('a')
                ->join('fa_oa_give b','a.log_id=b.id','left')
                ->join('fa_admin c','b.admin_id=c.id','left')
                ->join('fa_info d','b.info_id=d.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->where('a.status',10)
                ->where('a.type',0)
                ->count();
        }elseif ($type == 3){
            $data = Db::table('fa_oa_relation')
                ->alias('a')
                ->field('b.id,c.nickname,d.name,b.create_time,b.status')
                ->join('fa_oa_give b','a.log_id=b.id','left')
                ->join('fa_admin c','b.admin_id=c.id','left')
                ->join('fa_info d','b.info_id=d.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->where('a.status',10)
                ->where('a.type',1)
                ->limit($page_start,$limt)
                ->order('id','desc')
                ->select();

            $total = Db::table('fa_oa_relation')
                ->alias('a')
                ->join('fa_oa_give b','a.log_id=b.id','left')
                ->join('fa_admin c','b.admin_id=c.id','left')
                ->join('fa_info d','b.info_id=d.id','left')
                ->where('a.admin_id',$this->user_arr['id'])
                ->where('a.status',10)
                ->where('a.type',1)
                ->count();
        }


        if (!empty($data)){
            foreach ($data as $k=>$v){
                $data[$k]['nickname'] = $v['nickname'].'提交的赠送服务';
                $data[$k]['create_time'] = date('Y-m-d',$v['create_time']);

                if ($v['status'] == 0){
                    $data[$k]['status'] = '待审核';
                }elseif ($v['status'] == 1){
                    $data[$k]['status'] = '审核通过';
                }elseif ($v['status'] == 2){
                    $data[$k]['status'] = '审核驳回';
                }else{
                    $data[$k]['status'] = '';
                }
            }

        }


        //组装数据
        $datas = [
            'page' => $page,
            'total' => $total,
            'limt' => $limt,
            'list' => $data,
        ];

        $this->success('成功',$datas);
    }

    /**
     * 赠送服务申请详情
     *
     */
    public function give_detail(){
        $id = empty(request()->post('id')) ? $this->error('id不能为空'):request()->post('id');

        $data = Db::table('fa_oa_give')
            ->alias('a')
            ->field('a.id,a.admin_id,a.code,b.number,b.name,a.type,a.apply_money,a.create_time,a.ids,a.status as statuss')
            ->join('fa_info b','a.info_id=b.id','left')
            ->where('a.id',$id)
            ->find();

        if (!empty($data)){
            $data['create_time'] = date('Y-m-d H:i:s',$data['create_time']);

            $name = Db::table('fa_admin')
                ->field('id,nickname')
                ->where('id',$data['admin_id'])
                ->find();
            if (empty($name)){
                $data['names'] = '';
            }else{
                $data['names'] = $name['nickname'].'提交的赠送服务';
            }

            $data['detail'] = Db::table('fa_oa_services')
                ->alias('a')
                ->field('a.id,a.count,a.money,a.total,a.vip_id,b.name as serve')
                ->join('fa_vip_serve b','a.vip_id=b.id','left')
                ->where('a.oa_id',$data['id'])
                ->select();

            $data['send'] = Db::table('fa_oa_relation')
                ->alias('a')
                ->field('a.id,b.nickname,b.avatar')
                ->join('fa_admin b','a.admin_id=b.id','left')
                ->where(['a.log_id'=>$data['id'],'a.status'=>10,'a.type'=>1])
                ->select();

            $arr = Db::table('fa_admin')
                ->field('id,nickname,avatar')
                ->where('id','in',$data['ids'])
                ->select();

            $data['status'] = '';
            $arrs = [];
            foreach ($arr as $k=>$v){
                $pan = Db::table('fa_oa_relation')
                    ->where(['log_id'=>$data['id'],'admin_id'=>$v['id'],'status'=>10,'type'=>0])
                    ->find();

                if (empty($pan)){
                    $status = '';
                    $msg = '';
                    $create_time = '';
                }else{

                    if ($pan['is_comment'] == 0){
                        $status = '待审核';
                    }elseif ($pan['is_comment'] == 1){
                        $status = '审核通过';
                    }elseif ($pan['is_comment'] == 2){
                        $status = '审核驳回';
                    }else{
                        $status = '无';
                    }

                    if ($v['id'] == $this->user_arr['id']){
                        $data['status'] = $pan['is_comment'];
                    }


                    $msg = $pan['msg'];
                    if (empty($pan['update_time'])){
                        $create_time = '';
                    }else{
                        $create_time = date('Y-m-d H:i:s',$pan['update_time']);
                    }

                }

                $arrs[$k] = [
                    'nickname' => $v['nickname'],
                    'avatar' => $v['avatar'],
                    'create_time' => $create_time,
                    'status' => $status,
                    'msg' => $msg,
                ];
            }

            $adim_arr = Db::table('fa_admin')
                ->where('id',$data['admin_id'])
                ->find();


            $arr_z[0] = [
                'nickname' => $adim_arr['nickname'],
                'avatar' => $adim_arr['avatar'],
                'create_time' => $data['create_time'],
                'status' => '发起申请',
                'msg' => '',
            ];

            $arrss = array_merge($arr_z,$arrs);

            $data['examine'] = $arrss;
            unset($data['ids']);

        }

        $this->success('成功',$data);

    }


    /**
     * 公告列表接口
     *
     */
    public function notice_list(){
        $page = empty(request()->post('page')) ? 1:request()->post('page');//获取页码
        $limt = empty(request()->post('limt')) ? 10:request()->post('limt');//获取每页显示条数

        $type = empty(request()->post('type')) ? '':request()->post('type');

        //分页
        if ($page == 1) {
            $page_start = 0;
            $page_end = $limt-1;
        } else {
            $page_start = ($page - 1) * $limt;
            $page_end = $page_start + $limt-1;
        }

        $where = [];
        if (!empty($type)){
            $where['a.type'] = $type;
        }

        $data = Db::table('fa_oa_relevance')
            ->alias('a')
            ->field('a.id,b.title,a.create_time,a.type,b.content')
            ->join('fa_oa_notice b','a.notice_id=b.id','left')
            ->where('a.admin_id',$this->user_arr['id'])
            ->where($where)
            ->limit($page_start,$limt)
            ->order('a.id','desc')
            ->select();

        $total = Db::table('fa_oa_relevance')
            ->alias('a')
            ->join('fa_oa_notice b','a.notice_id=b.id','left')
            ->where('a.admin_id',$this->user_arr['id'])
            ->where($where)
            ->count();

        if (!empty($data)){
            foreach ($data as $k=>$v){
                $data[$k]['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
            }
        }

        //组装数据
        $datas = [
            'page' => $page,
            'total' => $total,
            'limt' => $limt,
            'list' => $data,
        ];

        $this->success('成功',$datas);
    }

    /**
     * 公告详情
     *
     */
    public function notice_detail(){
        $id = empty(request()->post('id')) ? $this->error('id不能为空'):request()->post('id');

        $data = Db::table('fa_oa_relevance')
            ->alias('a')
            ->field('a.id,b.title,b.content')
            ->join('fa_oa_notice b','a.notice_id=b.id','left')
            ->where('a.id',$id)
            ->find();

        $this->success('成功',$data);
    }

    /**
     * 改变公告状态
     *
     */
    public function notice_type(){
        $id = empty(request()->post('id')) ? $this->error('id不能为空'):request()->post('id');

        $arr = Db::table('fa_oa_relevance')
            ->where('id',$id)
            ->find();

        if (empty($arr)){
            $this->error('公告数据为空');
        }

        if ($arr['admin_id'] != $this->user_arr['id']){
            $this->error('用户id错误');
        }

        $update = Db::table('fa_oa_relevance')
            ->where('id',$id)
            ->update([
                'type' => 2,
                'update_time' => time(),
            ]);

        if ($update !== false){
            $this->success('成功');
        }else{
            $this->error('失败');
        }
    }


    /**
     * 添加就诊人信息接口
     *
     */
    public function add_info(){
        $users_id = empty(request()->post('id')) ? '':request()->post('id');//获取id

        $name = empty(request()->post('name')) ? $this->error('name不能为空'):request()->post('name');//获取name
        $sex = empty(request()->post('sex')) ? $this->error('sex不能为空'):request()->post('sex');//获取sex
        $nation = empty(request()->post('nation')) ? '':request()->post('nation');//获取nation
        $id_card = empty(request()->post('id_card')) ? '':request()->post('id_card');//获取id_card
        $mobile = empty(request()->post('mobile')) ? '':request()->post('mobile');//获取mobile
        $relation = empty(request()->post('relation')) ? '':request()->post('relation');//获取relation
        $nationality = empty(request()->post('nationality')) ? '':request()->post('nationality');//获取nationality
        $birthday = empty(request()->post('birthday')) ? $this->error('birthday不能为空'):request()->post('birthday');//获取birthday
        $birthplace = empty(request()->post('birthplace')) ? '':request()->post('birthplace');//获取birthplace
        $address = empty(request()->post('address')) ? '':request()->post('address');//获取address

        $binding = empty(request()->post('binding')) ? '':request()->post('binding');

        $position = empty(request()->post('position')) ? $this->error('position不能为空'):request()->post('position');

        //判断手机号格式
        if (!empty($mobile)){
            $mobile = is_phone($mobile);

            if ($mobile === false){
                $this->error('手机号格式错误');
            }
        }

        //判断身份证是否为空
        if(empty($id_card)){
            $status = 1;
            $id_card = 'A'.get_strings(16,2);
        }else{
            $status = 0;
        }

        if(empty($users_id)) {
            $status = 1;
        }else{
            $status = 0;
        } //新增


        $is_in = Db::table('fa_info')
            ->where(['id_card'=>$id_card])
            ->find();

        //判断主微信id是否为空
        if (empty($users_id)){
            if(!empty($is_in)){
                $this->error('身份证已存在,请重新请求');
            }

            $number = 'looin'.get_strings(6,2);
            $insert = Db::table('fa_info')
                ->insertGetId([
                    'user_id' => $users_id,
                    'name' => $name,
                    'sex' => $sex,
                    'nation' => $nation,
                    'id_card' => $id_card,
                    'mobile' => $mobile,
                    'relation' => $relation,
                    'nationality' => $nationality,
                    'birthday' => $birthday,
                    'birthplace' => $birthplace,
                    'address' => $address,
                    'number' => $number,
                    'create_time' => time(),
                    'binding' => $binding,
                    'status' => $status,
                    'position' => $position,
                ]);


            if($insert !== false){
                $this->success('成功',[]);
            }else{
                $this->error('失败',[]);
            }


        }


        $users_arr = Db::table('fa_users')
            ->where('id',$users_id)
            ->find();

        if (empty($users_arr)){
            $this->error('绑定主微信账号数据为空');
        }

        if ($users_arr['pid'] != 0){
            $this->error('该用户不能添加就诊人');
        }

        $pan = Db::table('fa_info')
            ->where('user_id',$users_arr['id'])
            ->count();
        //判断会员添加次数

        if ($users_arr['count'] < $pan+1){
            $this->error('该用户只能添加'.$users_arr['count'].'个就诊信息');
        }


        if ($relation == '本人'){
            //判断关系、本人
            $relation_pan = Db::table('fa_info')
                ->where(['user_id'=>$users_arr['id'],'relation'=>'本人'])
                ->count();

            if ($relation_pan > 0){
                $this->error('已存在本人,请重新选择');
            }
        }


        if ($relation == '配偶'){
            //判断关系、配偶
            $relations = Db::table('fa_info')
                ->where(['user_id'=>$users_arr['id'],'relation'=>'配偶'])
                ->count();

            if ($relations > 0){
                $this->error('已存在配偶,请重新选择');
            }
        }


        $card = Db::table('fa_info')
            ->where('id_card',$id_card)
            ->find();

        if ($card['is_del'] != 1){
            if (!empty($card)){
                $this->error('该身份证已存在,请重新填写');
            }
        }



        //开启事务
        Db::startTrans();

        if (!empty($binding)){
            $binding_arr = Db::table('fa_users')
                ->where('id',$binding)
                ->find();

            $pan_arrs = Db::table('fa_info')
                ->where('user_id',$binding)
                ->count();

            if ($pan_arrs > 0){
                $this->error('该账户不能绑定');
            }

            if (empty($binding_arr)){
                $this->error('绑定用户数据为空');
            }

            if ($binding_arr['pid'] != 0){
                $this->error('该用户已被绑定');
            }

            if ($binding == $users_arr['id']){
                $update = 1;
            }else{
                $update = Db::table('fa_users')
                    ->where('id',$binding)
                    ->update([
                        'level' => $users_arr['level'],
                        'pid' => $users_arr['id'],
                        'vip_time' => $users_arr['vip_time'],
                        'count' => $users_arr['count'],
                        'update_time' => time(),
                    ]);
            }




        }else{
            $update = 1;
        }


        if (empty($is_in)){
            $number = 'looin'.get_strings(6,2);
            $insert = Db::table('fa_info')
                ->insertGetId([
                    'user_id' => $users_arr['id'],
                    'name' => $name,
                    'sex' => $sex,
                    'nation' => $nation,
                    'id_card' => $id_card,
                    'mobile' => $mobile,
                    'relation' => $relation,
                    'nationality' => $nationality,
                    'birthday' => $birthday,
                    'birthplace' => $birthplace,
                    'address' => $address,
                    'number' => $number,
                    'create_time' => time(),
                    'binding' => $binding,
                    'status' => $status,
                    'position' => $position,
                ]);
        }else{
            $insert = Db::table('fa_info')
                ->where('id',$is_in['id'])
                ->update([
                    'user_id' => $users_arr['id'],
                    'name' => $name,
                    'sex' => $sex,
                    'nation' => $nation,
                    //'id_card' => $id_card,
                    'mobile' => $mobile,
                    'relation' => $relation,
                    'nationality' => $nationality,
                    'birthday' => $birthday,
                    'birthplace' => $birthplace,
                    'address' => $address,
                    //'number' => $number,
                    'update_time' => time(),
                    'binding' => $binding,
                    'is_del' => 0,
                    'position' => $position,
                ]);
        }


        if (($insert !== false) && ($update !== false)){
            //事务提交
            Db::commit();
            $this->success('添加成功',[]);
        }else{
            //事务回滚
            Db::rollback();
            $this->error('添加失败');
        }

    }

    /**
     * 新增挂号
     *
     */
    public function add_registration(){
        $info_id = empty(request()->post('info_id')) ? $this->error('info_id不能为空'):request()->post('info_id');
        $project = empty(request()->post('project')) ? $this->error('project不能为空'):request()->post('project');
        $project_ids = empty(request()->post('project_ids')) ? $this->error('project_ids不能为空'):request()->post('project_ids');

        $time = empty(request()->post('time')) ? $this->error('time不能为空'):request()->post('time');
        $msg = empty(request()->post('msg')) ? '':request()->post('msg');
        $date = empty(request()->post('date')) ? $this->error('date不能为空'):request()->post('date');
        $medic_id = empty(request()->post('medic_id')) ? $this->error('medic_id不能为空'):request()->post('medic_id');

        $info_arr = Db::table('fa_info')
            ->where('id',$info_id)
            ->find();

        if (empty($info_arr)){
            $this->error('就诊人数据为空',[]);
        }

        if ($info_arr['binding'] != 0){
            $users_id = $info_arr['binding'];
        }else{
            $users_id =  $info_arr['user_id'];
        }

        $medic_arr = Db::table('fa_medic')
            ->where('id',$medic_id)
            ->find();

        if(empty($medic_arr)){
            $this->error('专家数据为空',[]);
        }

        if($medic_arr['type'] != '专家'){
            $this->error('专家数据错误',[]);
        }

        if (!in_array($time,[1,2,3,4,5,6])){
            $this->error('time参数错误',[]);
        }

        if ($date < date('Y-m-d')){
            $this->error('选择时间不能小于当前时间',[]);
        }

        $pan = Db::table('fa_registration')
            ->alias('a')
            ->join('fa_working b','a.working_id=b.id','left')
            ->where(['b.date'=>$date,'b.time'=>$time,'b.medic_id'=>$medic_id])
            ->find();

        if (!empty($pan)){
            $this->error('该时间段已被预约',[]);
        }

        switch ($time){
            case 1:
                if (date('Y-m-d H:i:s') > $date.' 10:00:00'){
                    $this->error('预约时间已超时');
                }
                break;
            case 2:
                if (date('Y-m-d H:i:s') > $date.' 11:00:00'){
                    $this->error('预约时间已超时');
                }
                break;
            case 3:
                if (date('Y-m-d H:i:s') > $date.' 12:00:00'){
                    $this->error('预约时间已超时');
                }
                break;
            case 4:
                if (date('Y-m-d H:i:s') > $date.' 15:00:00'){
                    $this->error('预约时间已超时');
                }
                break;
            case 5:
                if (date('Y-m-d H:i:s') > $date.' 16:00:00'){
                    $this->error('预约时间已超时');
                }
                break;
            case 6:
                if (date('Y-m-d H:i:s') > $date.' 17:00:00'){
                    $this->error('预约时间已超时');
                }
                break;
            default:
                $this->error('预约时间错误');
                break;
        }

        $dates = date("w",strtotime($date));

        switch ($time){
            case 1:
                $tims = '09:00-10:00';
                break;
            case 2:
                $tims = '10:00-11:00';
                break;
            case 3:
                $tims = '11:00-12:00';
                break;
            case 4:
                $tims = '14:00-15:00';
                break;
            case 5:
                $tims = '15:00-16:00';
                break;
            case 6:
                $tims = '16:00-17:00';
                break;
        }

        switch ($dates){
            case 0:
                $day = '(星期天)';
                break;
            case 1:
                $day = '(星期一)';
                break;
            case 2:
                $day = '(星期二)';
                break;
            case 3:
                $day = '(星期三)';
                break;
            case 4:
                $day = '(星期四)';
                break;
            case 5:
                $day = '(星期五)';
                break;
            case 6:
                $day = '(星期六)';
                break;
            default:
                $day = '';
                break;
        }

        $timss = explode('-',$tims);

        $visiting_time = $date.' '.$day.' '.$tims;
        $begin_time = strtotime($date.' '.$timss[0]);
        $end_time = strtotime($date.' '.$timss[1]);

        //开启事务
        Db::startTrans();

        $working_id = Db::table('fa_working')
            ->insertGetId([
                'medic_id' => $medic_id,
                'date' => $date,
                'time' => $time,
                'create_time' => time(),
            ]);

        $insert = Db::table('fa_registration')
            ->insertGetId([
                'user_id' => $users_id,
                'info_id' => $info_id,
                'medic_id' => $medic_id,
                'project' => $project,
                'project_ids' => $project_ids,
                'visiting_time' => $visiting_time,
                'begin_time' => $begin_time,
                'end_time' => $end_time,
                'status' => '待就诊',
                'price' => $medic_arr['registration_fee']*100,
                'create_time' =>time(),
                'code' =>get_strings(16,2),
                'working_id' =>$working_id,
                'date' =>$date,
                'msg' =>$msg,
                'channel' =>1,
            ]);

        if (($working_id > 0) && ($insert !== false)){
            //提交事务
            Db::commit();

            if (empty($info_arr)){
                $this->success('成功,就诊人数据为空',[]);
            }

            //$sms = send_sms('SMS_21070800025',$info_arr['mobile'],['time'=>$visiting_time]);
            $sms = send_sms('SMS_21090100012',$info_arr['mobile'],['time'=>$visiting_time]);
            //$sms= 1;


            if ($sms != 1){
                //$this->error('发送失败',[]);
                $this->success('成功,短信推送失败',[]);
            }else{
                //$this->success('发送成功',[]);
                $this->success('成功',[]);
            }

            $this->success('成功',[]);

        }else{
            //事务回滚
            Db::rollback();
            $this->error('失败',[]);
        }




    }


    /**
     * 编辑就诊人
     *
     */
    public function edit_info(){
        $id = empty(request()->post('id')) ? $this->error('id不能为空'):request()->post('id');//获取id
        $users_id = empty(request()->post('user_id')) ? '':request()->post('user_id');//获取users_id

        $name = empty(request()->post('name')) ? $this->error('name不能为空'):request()->post('name');//获取name
        $sex = empty(request()->post('sex')) ? $this->error('sex不能为空'):request()->post('sex');//获取sex
        $id_card = empty(request()->post('id_card')) ? $this->error('id_card不能为空'):request()->post('id_card');//获取id_card
        $birthday = empty(request()->post('birthday')) ? $this->error('birthday不能为空'):request()->post('birthday');//获取birthday

        $nation = empty(request()->post('nation')) ? '':request()->post('nation');//获取nation
        $mobile = empty(request()->post('mobile')) ? '':request()->post('mobile');//获取mobile
        $relation = empty(request()->post('relation')) ? '':request()->post('relation');//获取relation
        $nationality = empty(request()->post('nationality')) ? '':request()->post('nationality');//获取nationality
        $birthplace = empty(request()->post('birthplace')) ? '':request()->post('birthplace');//获取birthplace
        $address = empty(request()->post('address')) ? '':request()->post('address');//获取address

        //判断手机号格式
        if (!empty($mobile)){
            $mobile = is_phone($mobile);

            if ($mobile === false){
                $this->error('手机号格式错误');
            }
        }


        $user_arr = Db::table('fa_info')
            ->where('id',$id)
            ->find();

        if (empty($user_arr)){
            $this->error('就诊人数据为空');
        }
        if ($user_arr['id_card'] == $id_card){
            $update = Db::table('fa_info')
                ->where('id',$id)
                ->update([
                    'name' => $name,
                    'sex' => $sex,
                    'nation' => $nation,
                    //'id_card' => $id_card,
                    'mobile' => $mobile,
                    'relation' => $relation,
                    'nationality' => $nationality,
                    'birthday' => $birthday,
                    'birthplace' => $birthplace,
                    'address' => $address,
                    'update_time' => time(),
                    'user_id' => $users_id,
                ]);
        }else{
            $is_in = Db::table('fa_info')
                ->where(['id_card'=>$id_card])
                ->find();

            if (!empty($is_in)){
                $this->error('身份证已存在');
            }

            $update = Db::table('fa_info')
                ->where('id',$id)
                ->update([
                    'name' => $name,
                    'sex' => $sex,
                    'nation' => $nation,
                    'id_card' => $id_card,
                    'mobile' => $mobile,
                    'relation' => $relation,
                    'nationality' => $nationality,
                    'birthday' => $birthday,
                    'birthplace' => $birthplace,
                    'address' => $address,
                    'update_time' => time(),
                    'user_id' => $users_id,
                ]);
        }

        if ($update !== false){
            $this->success('成功');
        }else{
            $this->error('失败');
        }

    }


    /**
     * 修改状态-变成正常客户
     *
     */
    public function status_info(){
        $id = empty(request()->post('id')) ? $this->error('id不能为空'):request()->post('id');//获取id
        $users_id = empty(request()->post('user_id')) ? $this->error('user_id不能为空'):request()->post('user_id');//获取users_id

        $info_arr = Db::table('fa_info')
            ->where('id',$id)
            ->find();

        if (empty($info_arr)){
            $this->error('就诊人数据为空');
        }

        $users_arr = Db::table('fa_users')
            ->where('id',$users_id)
            ->find();

        if (empty($users_arr)){
            $this->error('微信数据为空');
        }

        if ($users_arr['pid'] != 0){
            $this->error('该用户不能添加就诊人');
        }

        $pan = Db::table('fa_info')
            ->where('user_id',$users_arr['id'])
            ->count();
        //判断会员添加次数

        if ($users_arr['count'] < $pan+1){
            $this->error('该用户只能添加'.$users_arr['count'].'个就诊信息');
        }


        if ($info_arr['relation'] == '本人'){
            //判断关系、本人
            $relation_pan = Db::table('fa_info')
                ->where(['user_id'=>$users_arr['id'],'relation'=>'本人'])
                ->count();

            if ($relation_pan > 0){
                $this->error('已存在本人,请重新选择');
            }
        }


        if ($info_arr['relation'] == '配偶'){
            //判断关系、配偶
            $relations = Db::table('fa_info')
                ->where(['user_id'=>$users_arr['id'],'relation'=>'配偶'])
                ->count();

            if ($relations > 0){
                $this->error('已存在配偶,请重新选择');
            }
        }


        $update = Db::table('fa_info')
            ->where('id',$id)
            ->update([
                'user_id' => $users_id,
                'status' => 0,
                'update_time' => time(),
            ]);

        if ($update !== false){
            $this->success('成功');
        }else{
            $this->error('失败');
        }

    }

    /**
     * 更换会员健康档案头像
     *
     */
    public function image(){
        $id = empty(request()->post('id')) ? $this->error('id不能为空'):request()->post('id');
        $image = empty(request()->post('image')) ? $this->error('image不能为空'):request()->post('image');

        $update = Db::table('fa_info')
            ->where('id',$id)
            ->update([
                'image' => $image,
                'update_time' => time(),
            ]);

        if ($update !== false){
            $this->success('成功');
        }else{
            $this->error('失败');
        }
    }



}
